function is_utf8($string) {
return preg_match('%^(?:
[x09x0Ax0Dx20-x7E] -xBF] xBF][x80-xBF] # excluding overlongs
# excluding surrogates
x90-xBF][x80-xBF]{2} 80-xBF] {2} # plane 16
)*$%xs', $string);
}
The accuracy is basically the same as mb_detect_encoding, both correct and wrong.
Encoding detection cannot be 100% accurate, this thing can basically meet the requirements.
The above introduces the string array code to detect whether a string is utf8 encoded under PHP, including the content of string array. I hope it will be helpful to friends who are interested in PHP tutorials.