php中mb_convert_encoding 和 iconv 的区别

作者:袖梨 2022-06-24

比如你用Zend Studio或Editplus写程序时,用的是gbk编码,如果数据需要入数据库,而数据库的编码为utf8时,这时就要把数据进行编码转换,不然进到数据库就会变成乱码。

mb_convert_encoding的用法见官方:

mb_convert_encoding — Convert character encoding

Report a bug 说明
string mb_convert_encoding ( string $str , string $to_encoding [, mixed $from_encoding ] )
Converts the character encoding of string str to to_encoding from optionally from_encoding.

Report a bug 参数

str
The string being encoded.

to_encoding
The type of encoding that str is being converted to.

from_encoding
Is specified by character code names before conversion. It is either an array, or a comma separated enumerated list. If from_encoding is not specified, the internal encoding will be used.

See supported encodings.


Report a bug 返回值
The encoded string.

Report a bug 范例

Example #1 mb_convert_encoding() example

 代码如下 复制代码

/* Convert internal character encoding to SJIS */
$str = mb_convert_encoding($str, "SJIS");

/* Convert EUC-JP to UTF-7 */
$str = mb_convert_encoding($str, "UTF-7", "EUC-JP");

/* Auto detect encoding from JIS, eucjp-win, sjis-win, then convert str to UCS-2LE */
$str = mb_convert_encoding($str, "UCS-2LE", "JIS, eucjp-win, sjis-win");

/* "auto" is expanded to "ASCII,JIS,UTF-8,EUC-JP,SJIS" */
$str = mb_convert_encoding($str, "EUC-JP", "auto");
?>

mb_convert_encoding( $str, $encoding1,$encoding2 )

$str,要转换编码的字符串
$encoding1,目标编码,如utf-8,gbk,大小写均可
$encoding2,原编码,如utf-8,gbk,大小写均可

实例1

 代码如下 复制代码
$str='脚本之家:http://www.111com.net';
echo mb_convert_encoding($str, "UTF-8"); //编码转换为utf-8
?>

 

 代码如下 复制代码
$str='脚本之家:http://www.111com.net';
echo mb_convert_encoding($str, "UTF-8", "GBK"); //已知原编码为GBK,转换为utf-8
?>
 代码如下 复制代码
$str='脚本之家:http://www.111com.net';
echo mb_convert_encoding($str, "UTF-8", "auto"); //未知原编码,通过auto自动检测后,转换编码为utf-8
?>

做一个GBK To UTF-8

 

 代码如下 复制代码
< ?php
header("content-Type: text/html; charset=Utf-8");
echo mb_convert_encoding("???S我的友仔", "UTF-8", "GBK");
?>

再来个GB2312 To Big5

 

 代码如下 复制代码
< ?php
header("content-Type: text/html; charset=big5");
echo mb_convert_encoding("你是我的朋友", "big5", "GB2312");
?>

不过要使用上面的函数需要安装但是需要先enable mbstring 扩展库。

PHP中的另外一个函数iconv也是用来转换字符串编码的,与上函数功能相似。

下面还有一些详细的例子:

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] )
需要先enable mbstring 扩展库,在 php.ini里将; extension=php_mbstring.dll 前面的 ; 去掉
mb_convert_encoding 可以指定多种输入编码,它会根据内容自动识别,但是执行效率比iconv差太多;


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


使用:

发现iconv在转换字符”—”到gb2312时会出错,如果没有ignore参数,所有该字符后面的字符串都无法被保存。不管怎么样,这个”—”都无法转换成功,无法输出。 另外mb_convert_encoding没有这个bug.

一般情况下用 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.

 代码如下 复制代码
/* Auto detect encoding from JIS, eucjp-win, sjis-win, then convert str to UCS-2LE */
$str = mb_convert_encoding($str, “UCS-2LE”, “JIS, eucjp-win, sjis-win”);
/* “auto” is expanded to “ASCII,JIS,UTF-8,EUC-JP,SJIS” */
$str = mb_convert_encoding($str, “EUC-JP”, “auto”);

例子:

 代码如下 复制代码
$content = iconv(”GBK”, “UTF-8//IGNORE″, $content);
$content = mb_convert_encoding($content, “UTF-8″, “GBK”);

 

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

相关文章

精彩推荐