Reputation: 1074
I am using iconv() to check if a string contains non-valid UTF-8 characters.
$valid = $string == iconv('UTF-8', 'UTF-8//IGNORE', $string);
However, this still throws the error: "iconv(): Detected an illegal character in input string"
To the best of my knowledge this should not be possible using the //IGNORE flag?
I'm using PHP 5.5.9-1ubuntu4.6 on Ubuntu 14.04.1 LTS
Upvotes: 3
Views: 3344
Reputation: 7409
Another answer provides a better answer for why iconv()
is throwing an error:
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.
Taken from a comment in the PHP manual, you can detect if a string is encoded in UTF-8 with this function:
$valid = mb_detect_encoding($str, 'UTF-8', true); // returns boolean.
More info on mb_detect_encoding();
Upvotes: 4