iconv UTF-8//IGNORE still produces "illegal character" error

Znarkus picture Znarkus · Feb 21, 2012 · Viewed 27.2k times · Source
$string = iconv("UTF-8", "UTF-8//IGNORE", $string);

I thought this code would remove invalid UTF-8 characters, but it produces [E_NOTICE] "iconv(): Detected an illegal character in input string". What am I missing, how do I properly strip a string from illegal characters?

Answer

msgmash.com picture msgmash.com · Feb 21, 2012

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.