Alex Rivera | Logout

How can I detect a malformed UTF-8 string in PHP?

Asked 2011-07-17T11:36:23.590
32

The iconv function sometimes gives me an error:

Notice:
iconv() [function.iconv]:
Detected an incomplete multibyte character in input string in [...]

Is there a way to detect that there are illegal characters in a UTF-8 string before sending data to inconv()?

Edit
Report

1 Answer

0

You could try using mb_detect_encoding to detect if you've got a different character set (than UTF-8) then mb_convert_encoding to convert to UTF-8 if required. It's more likely that people are giving you valid content in a different character set than giving you invalid UTF-8.

answered 2011-07-17T11:41:12.620

Your Answer