您的位置:首页 > 其它

不是iconv函数的bug

2010-01-16 12:57 176 查看
函数原型:string iconv ( string $in_charset , string $out_charset , string $str )

特别是第二个参数说明:

The output charset.

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 and an E_NOTICE is generated.

意思是如果你在第二个参数后面加上//TRANSLIT ,这样的话,当遇到目标输出的编码不认识的字符的时候,能被一个或几个近似的字符代替;而如果在第二个参数后面加上//IGNORE的时候,当目标输出的编码不认识的时候则什么也不操作;什么都不加的时候则会报错~~~
手册里面有个非常生动的例子:

<?php
$text = "This is the Euro symbol '€'.";

echo 'Original : ', $text, PHP_EOL;
echo 'TRANSLIT : ', iconv("UTF-8", "ISO-8859-1//TRANSLIT", $text), PHP_EOL;
echo 'IGNORE : ', iconv("UTF-8", "ISO-8859-1//IGNORE", $text), PHP_EOL;
echo 'Plain : ', iconv("UTF-8", "ISO-8859-1", $text), PHP_EOL;

?>

输出结果是:
Original : This is the Euro symbol ‘€’.

TRANSLIT : This is the Euro symbol ‘EUR’.

IGNORE : This is the Euro symbol ”.

Plain :

Notice: iconv(): Detected an illegal character in input string in ./iconv-example.php on line 7

This is the Euro symbol ‘
这样就非常清楚了

这里总结几点教训:

第一:php最好的手册就是官方的在线收藏;

(网上下的php中文手册往往会不完整或者没有更新,开始就是因为我电脑上的php中文手册没有这几个参数的说明)

第二:别太相信百度、google的中文搜索结果,别人讲的不一定是对的~~

最好就是直接找最权威,最官方的说明~~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: