iconv UTF-8//IGNORE still produces “illegal character” error

点点圈 提交于 2019-11-30 17:34:12
msgmash.com

The output character set (the second parameter) should be different from the input character set (first param). If they are the same, then if there are illegal UTF-8 characters in the string, iconv will reject them as being illegal according to the input character set.

I know 2 methods how to fix UTF-8 string containing illegal characters:

  1. Illegal characters will be replaced by question marks ("?"):

$message = mb_convert_encoding($message, 'UTF-8', 'UTF-8');

  1. Illegal characters will be removedL

$message = iconv('UTF-8', 'UTF-8//IGNORE', $message);

The second method actually was described in question. But it doesn't produce any E_NOTICE in my case. I tested with different corrupted UTF-8 strings with error_reporting(E_ALL); and always result was as expected. Possible something was changed since 2012. I tested on PHP 7.2.9 Win.

To simply ignore notice, you can use "@":

$string = @iconv("UTF-8", "UTF-8//IGNORE", $string);

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!