百科狗-知识改变命运!
--

mb_convert_encoding() - mb函数(多字节字符串转化库)

乐乐12个月前 (11-21)阅读数 18#技术干货
文章标签字符

mb_convert_encoding()

(PHP 4 >= 4.0.6, PHP 5, PHP 7)

转换字符的编码

说明

mb_convert_encoding(string $str,string $to_encoding[,mixed $from_encoding= mb_internal_encoding()]): string

将string类型$str的字符编码从可选的$from_encoding转换到$to_encoding。

参数

$str

要编码的string。

$to_encoding

$str要转换成的编码类型。

$from_encoding

在转换前通过字符代码名称来指定。它可以是一个array也可以是逗号分隔的枚举列表。如果没有提供$from_encoding,则会使用内部(internal)编码。

参见支持的编码。

返回值

编码后的string。

范例

mb_convert_encoding() - mb函数(多字节字符串转化库)

mb_convert_encoding()例子

参见

  • mb_detect_order()设置/获取字符编码的检测顺序
For my last project I needed to convert several CSV files from Windows-1250 to UTF-8, and after several days of searching around I found a function that is partially solved my problem, but it still has not transformed all the characters. So I made ​​this:
function w1250_to_utf8($text) {
  // map based on:
  // http://konfiguracja.c0.pl/iso02vscp1250en.html
  // http://konfiguracja.c0.pl/webpl/index_en.html#examp
  // http://www.htmlentities.com/html/entities/
  $map = array(
    chr(0x8A) => chr(0xA9),
    chr(0x8C) => chr(0xA6),
    chr(0x8D) => chr(0xAB),
    chr(0x8E) => chr(0xAE),
    chr(0x8F) => chr(0xAC),
    chr(0x9C) => chr(0xB6),
    chr(0x9D) => chr(0xBB),
    chr(0xA1) => chr(0xB7),
    chr(0xA5) => chr(0xA1),
    chr(0xBC) => chr(0xA5),
    chr(0x9F) => chr(0xBC),
    chr(0xB9) => chr(0xB1),
    chr(0x9A) => chr(0xB9),
    chr(0xBE) => chr(0xB5),
    chr(0x9E) => chr(0xBE),
    chr(0x80) => '€',
    chr(0x82) => '‚',
    chr(0x84) => '„',
    chr(0x85) => '…',
    chr(0x86) => '†',
    chr(0x87) => '‡',
    chr(0x89) => '‰',
    chr(0x8B) => '‹',
    chr(0x91) => '‘',
    chr(0x92) => '’',
    chr(0x93) => '“',
    chr(0x94) => '”',
    chr(0x95) => '•',
    chr(0x96) => '–',
    chr(0x97) => '—',
    chr(0x99) => '™',
    chr(0x9B) => '’',
    chr(0xA6) => '¦',
    chr(0xA9) => '©',
    chr(0xAB) => '«',
    chr(0xAE) => '®',
    chr(0xB1) => '±',
    chr(0xB5) => 'µ',
    chr(0xB6) => '¶',
    chr(0xB7) => '·',
    chr(0xBB) => '»',
  );
  return html_entity_decode(mb_convert_encoding(strtr($text, $map), 'UTF-8', 'ISO-8859-2'), ENT_QUOTES, 'UTF-8');
}
I've been trying to find the charset of a norwegian (with a lot of ø, æ, å) txt file written on a Mac, i've found it in this way:

The line that looks good, gives you the encoding it was written in.
Hope can help someone
aaron, to discard unsupported characters instead of printing a ?, you might as well simply set the configuration directive:
mbstring.substitute_character = "none"
in your php.ini. Be sure to include the quotes around none. Or at run-time with 
Hey guys. For everybody who's looking for a function that is converting an iso-string to utf8 or an utf8-string to iso, here's your solution:
public function encodeToUtf8($string) {
   return mb_convert_encoding($string, "UTF-8", mb_detect_encoding($string, "UTF-8, ISO-8859-1, ISO-8859-15", true));
}
public function encodeToIso($string) {
   return mb_convert_encoding($string, "ISO-8859-1", mb_detect_encoding($string, "UTF-8, ISO-8859-1, ISO-8859-15", true));
}
For me these functions are working fine. Give it a try
many people below talk about using 

to convert non-ascii code into html-readable stuff. Due to my webserver being out of my control, I was unable to set the database character set, and whenever PHP made a copy of my $s variable that it had pulled out of the database, it would convert it to nasty latin1 automatically and not leave it in it's beautiful UTF-8 glory.
So [insert korean characters here] turned into ?????.
I found myself needing to pass by reference (which of course is deprecated/nonexistent in recent versions of PHP)
so instead of

which worked perfectly until I upgraded, so I had to use

Hope it helps someone else out
My solution below was slightly incorrect, so here is the correct version (I posted at the end of a long day, never a good idea!)
Again, this is a quick and dirty solution to stop mb_convert_encoding from filling your string with question marks whenever it encounters an illegal character for the target encoding. 

Hope this helps someone! (Admins should feel free to delete my previous, incorrect, post for clarity)
-A
To add to the Flash conversion comment below, here's how I convert back from what I've stored in a database after converting from Flash HTML text field output, in order to load it back into a Flash HTML text field:
function htmltoflash($htmlstr)
{
 return str_replace("<br />","\n",
  str_replace("",">",
    mb_convert_encoding(html_entity_decode($htmlstr),
    "UTF-8","ISO-8859-1"))));
}
Why did you use the php html encode functions? mbstring has it's own Encoding which is (as far as I tested it) much more usefull:
HTML-ENTITIES
Example:
$text = mb_convert_encoding($text, 'HTML-ENTITIES', "UTF-8");
instead of ini_set(), you can try this
mb_substitute_character("none");
It appears that when dealing with an unknown "from encoding" the function will both throw an E_WARNING and proceed to convert the string from ISO-8859-1 to the "to encoding".
For those wanting to convert from $set to MacRoman, use iconv():

('macintosh' is the IANA name for the MacRoman character set.)
If you are trying to generate a CSV (with extended chars) to be opened at Exel for Mac, the only that worked for me was:

I also tried this:

But the first one didn't show extended chars correctly, and the second one, did't separe fields correctly
Clean a string for use as filename by simply replacing all unwanted characters with underscore (ASCII converts to 7bit). It removes slightly more chars than necessary. Hope its useful. 
$fileName = 'Test:!"$%&/()=ÖÄÜöäü

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)