Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

utf-8

iconv

$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?

like image 734
Znarkus Avatar asked Feb 21 '12 10:02

Znarkus


1 Answers

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.

like image 57
msgmash.com Avatar answered Oct 02 '22 12:10

msgmash.com