Home  >  Article  >  php教程  >  利用iconv()函数转字符编码的问题

利用iconv()函数转字符编码的问题

WBOY
WBOYOriginal
2016-06-06 20:11:04909browse

使用php的iconv函数库能完成各种字符集间的转换,是php编程中不可缺少的基础函数库。 但是在用iconv在转换一些特殊字符,例如—到gb2312时会出错。 先看下这个函数的用法: 最简单的应用,把gb2312置换成utf-8: $text=iconv(GB2312,UTF-8,$text); 在用$text

使用php的iconv函数库能完成各种字符集间的转换,是php编程中不可缺少的基础函数库。

但是在用iconv在转换一些特殊字符,例如”—”到gb2312时会出错。

先看下这个函数的用法:

最简单的应用,把gb2312置换成utf-8:
$text=iconv(“GB2312″,”UTF-8″,$text);

在用$text=iconv(“UTF-8″,”GB2312″,$text)过程中,如果遇到一些特别字符时,如:”—”,英文名中的”.”等等字符,转换就断掉了。这些字符后的文字都没法继续转换了。

针对这的问题,可以用如下代码实现:
$text=iconv(“UTF-8″,”GBK”,$text);

你没有看错,就这么简单,不使用gb2312,而写成GBK,就可以了。

还有一种方法,第二个参数,加上//IGNORE,忽略错误,如下:
iconv(“UTF-8″,”GB2312//IGNORE”,$data);

没有具体比较这两种方法,感觉第一种(GBK代替gb2312)方法更好。

php手册中iconv() 说明:

?iconv
?(PHP 4 >= 4.0.5, PHP 5)
?iconv – Convert string to requested character encoding
?Description
?string iconv ( string in_charset, string out_charset, string str )
?Performs a character set conversion on the string str from in_charset to out_charset. Returns the converted string or FALSE on failure.
?If you append the string //TRANSLIT to out_charset transliteration is activated. This means that when a character can't be represented in the target charset, it can be approximated through one or several similarly looking characters. If you append the string //IGNORE, characters that cannot be represented in the target charset are silently discarded. Otherwise, str is cut from the first illegal character.

在使用这个函数进行字符串编码转换时,需要注意,如果将utf-8转换为gb2312时,可能会出现字符串被截断的情况发生。此时可以使用以下方法解决:
$str=iconv(‘utf-8′,”gb2312//TRANSLIT”,file_get_contents($filepath));

即在第二个参数出添加红色字部分,表示:如果在目标编码中找不到与源编码相匹配的字符,会选择相似的字符进行转换。此处也可以使用://IGNORE 这个参数,表示忽略不能转换的字符。

ignore的意思是忽略转换时的错误,如果没有ignore参数,所有该字符后面的字符串都无法被保存。

iconv不是php的默认函数,也是默认安装的模块。需要安装才能用的。

如果是windows2000+php,你可以修改php.ini文件,将extension=php_iconv.dll前的”;”去掉,同时你要copy你的原php安装文件下的iconv.dll到你的winnt/system32下(如果你的dll指向的是这个目录)。在linux环境下,用静态安装的方式,在configure时加多一项 –with-iconv就可以了,phpinfo看得到iconv的项。(Linux7.3+Apache4.06+php4.3.2)。
mb_convert_encoding与iconv函数介绍

mb_convert_encoding这个函数是用来转换编码的。原来一直对程序编码这一概念不理解,不过现在好像有点开窍了。不过英文一般不会存在编码问题,只有中文数据才会有这个问题。比如你用Zend Studio或Editplus写程序时,用的是gbk编码,如果数据需要入数据库,而数据库的编码为utf8时,这时就要把数据进行编码转换,不然进到数据库就会变成乱码。

做一个GBK To UTF-8:

<?php header("content-Type: text/html; charset=Utf-8");
    echo mb_convert_encoding("图样图森破", "UTF-8", "GBK");
?>

再来个GB2312 To Big5:

<?php header("content-Type: text/html; charset=big5");
echo mb_convert_encoding("神马东东", "big5", "GB2312");
?>

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

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 会忽略掉不能转化的字符,而默认效果是从第一个非法字符截断。

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

$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