2018/03/15

PHP中Notice: iconv(): Unknown error (84) 的解决办法(转)


本文转自:
作者:Tony的技术空间
博客:[http://www.tonitech.com/822.html)

今天在工作的时候读取一个接口的数据使用了iconv转换字符编码格式 iconv('gb2312','utf-8', serialize($storeData)); 的时候出现了如下错误:

Notice: iconv(): Unknown error (84) 。。。。。。

读其官方文档 http://www.php.net/manual/en/function.iconv.php对参数out_charset的解释

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 到 out_charset 的参数后面,意味着如果找不到目标编码,则程序会去找与其相近的编码。如果你加的是 //IGNORE,则不会去找相近的编码,而且只要有一个字符是程序无法识别的则将会报错。

根据上面的解释我将代码

iconv('gb2312','utf-8', serialize($storeData));

改为

iconv('gb2312','utf-8//TRANSLIT//IGNORE', serialize($storeData));

这样就ok了!