Home > php教程 > php手册 > body text

php中iconv函数的一个小bug

WBOY
Release: 2016-06-06 19:36:21
Original
1071 people have browsed it

iconv转换字符集很好用,但是有时候你会发现iconv转换的时候会返回false或者空字符串,严格说来这算不上是iconv的问题,这其实是字符集的问题,但是实际编码中应该算是iconv的bug了。 你可以试试把以下这段字符串用iconv转换一下看看效果。 $str="www.bloggu

iconv转换字符集很好用,但是有时候你会发现iconv转换的时候会返回false或者空字符串,严格说来这算不上是iconv的问题,这其实是字符集的问题,但是实际编码中应该算是iconv的bug了。

你可以试试把以下这段字符串用iconv转换一下看看效果。

 $str="www.blogguy.cn,ok!the string is € .我要转换他!⊙●○①⊕◎Θ⊙¤㊣";
 echo '没有任何参数 : ', iconv("utf-8", "gbk", $str)."
";

在我的电脑中返回的结果是第一个特殊字符以后的字符都不能显示。

iconv有两个参数:分别是TRANSLIT 和IGNORE   ,分别的含义是TRANSLIT 表示如果目标编码里面不能翻译原编码,那么就直接找一个类似的最相近的字符或者字符串替代,例如€可能会被替换为EUR;而IGNORE   遇到目标字符集不能翻译的原始字符则直接跳过忽略,不返回false。

使用方法如下:

$infocontent=iconv("utf-8","gbk//IGNORE",$infocontent);

或者

$infocontent=iconv("utf-8","gbk//TRANSLIT",$infocontent);

鉴于此,我认为iconv应该提供一个默认的参数IGNORE,避免开发者使用不善时出现返回空字符串的情况。

一点小记录。

 

http://www.blogguy.cn/show-797-1.html

Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!