Home  >  Article  >  php教程  >  php 转换字符串编码 iconv与mb_convert_encoding的区别说明

php 转换字符串编码 iconv与mb_convert_encoding的区别说明

WBOY
WBOYOriginal
2016-06-06 20:39:231016browse

php 转换字符串编码 iconv与mb_convert_encoding的区别说明,需要的朋友可以参考下。

iconv — Convert string to requested character encoding(PHP 4 >= 4.0.5, PHP 5)
mb_convert_encoding — Convert character encoding(PHP 4 >= 4.0.6, PHP 5)

用法:
string mb_convert_encoding ( string str, string to_encoding [, mixed from_encoding] )
需要先启用 mbstring 扩展库,在 php.ini里将; extension=php_mbstring.dll 前面的 ; 去掉


string iconv ( string in_charset, string out_charset, string str )
注意:
第二个参数,除了可以指定要转化到的编码以外,还可以增加两个后缀://TRANSLIT 和 //IGNORE,
其中:
//TRANSLIT 会自动将不能直接转化的字符变成一个或多个近似的字符,
//IGNORE 会忽略掉不能转化的字符,而默认效果是从第一个非法字符截断。
Returns the converted string or FALSE on failure.

使用:
1. 发现iconv在转换字符"-"到gb2312时会出错,如果没有ignore参数,所有该字符后面的字符串都无法被保存。不管怎么样,这

个"-"都无法转换成功,无法输出。另外mb_convert_encoding没有这个bug.
2. mb_convert_encoding 可以指定多种输入编码,它会根据内容自动识别,但是执行效率比iconv差太多;如:$str =

mb_convert_encoding($str,"euc-jp","ASCII,JIS,EUC-JP,SJIS,UTF- 8");“ASCII,JIS,EUC-JP,SJIS,UTF-8”的顺序不同效果也有

差异
3. 一般情况下用 iconv,只有当遇到无法确定原编码是何种编码,或者iconv转化后无法正常显示时才用mb_convert_encoding 函数

.
from_encoding is specified by character code name before conversion. it can be array or string - comma separated

enumerated list. If it is not specified, the internal encoding will be used.

$str = mb_convert_encoding($str, "UCS-2LE", "JIS, eucjp-win, sjis-win");

$str = mb_convert_encoding($str, "EUC-JP', "auto");

例子:
$content = iconv("GBK", "UTF-8", $content);
$content = mb_convert_encoding($content, "UTF-8", "GBK");
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn