First, note that it is not possible to detect whether text belongs to a specific undesired encoding. You can only check whether a string is valid in a given encoding.

You can make use of the UTF-8 validity check that is available in preg_match since PHP 4.3.5. It will return false (with no additional information) if an invalid string is given:

Another possibility is mb_check_encoding:

Another function you can use is mb_detect_encoding:

It’s important to set the strict parameter to true.

Additionally, iconv allows you to change/drop invalid sequences on the fly. (However, if iconv encounters such a sequence, it generates a notification; this behavior cannot be changed.)

You can use @ and check the length of the return string:
Check the examples on the iconv manual page as well.

Leave a Reply

Your email address will not be published. Required fields are marked *