7 May 2014

This function checks whether a string is valid UTF-8. You can ensure that the string you operate is a valid UTF-8 string. preg_match fails on strings containing invalid UTF-8 byte sequences. It does not reject character codes above U+10FFFF represented by 4 or more octets.

Source code viewer
  1. if (strlen($text) == 0 XOR preg_match('/^./us', $text) == 1) {
  2. // The string validates.
  3. }
  4. else {
  5. // The string doesn't validate.
  6. }
Programming Language: PHP