String > Invalid UTF-8 character Favorite


Matches text that is not valid UTF-8. It can be used to replace or remove bad characters from a UTF-8 encoded string.


Note: Because the regex tester only accepts UTF-8 encoded strings, this will never match any text in the online tester. But it really does match invalid UTF-8 bytes in a string.

Sample PHP code:

preg_replace('/.../', '', $str); // remove
preg_replace('/.../', "\xEF\xBF\xBD", $str); // replace with Unicode Replacement Character (U+FFFD)
if (preg_match('/.../', $str, $matches, PREG_OFFSET_CAPTURE)) {
    $str = substr($str, 0, $matches[0][1]); // truncate string at first invalid character