-
- /**
- * Determine string encoding
- * edit by bbs.it-home.org
- */
- function is_utf8($word)
- {
- if(preg_match("/^([".chr(228)."-".chr( 233)."]developed[".chr(128)."-".chr(191)."]developed[".chr(128)."-".chr(191)."]developed)developed/" ,$word) == true || preg_match("/([".chr(228)."-".chr(233)."]developed[".chr(128)."-".chr(191) ."]developed[".chr(128)."-".chr(191)."]developed)developed$/",$word) == true || preg_match("/([".chr(228) ."-".chr(233)."]Developed[".chr(128)."-".chr(191)."]Developed[".chr(128)."-".chr(191). "]Developed){2,}/",$word) == true) {
- return true;
- }else {
- return false;
- }
- }
- $t = 'wangbin';
- //$t = iconv ('GB2312','UTF-8',$t)
- var_dump(is_utf8($t));
- ?>
Copy code
In addition, the function mb_detect_encoding in PHP can also implement such a function .
Detect whether the string is utf8 encoded code under PHP, function: mb_detect_encoding, this requires the mb_string library to be installed in the PHP environment.
For related information about the mb_detect_encoding function, you can refer to:
php function mb_detect_encoding to obtain string encoding
php mb_detect_encoding detects the problem of incorrect string encoding
The implemented function is as follows:
-
-
- /**
- * Check whether it is utf8 encoding
- * edit by bbs.it-home.org
- */
- function is_utf8($string) {
- return preg_match('%^(?:
- [x09x0Ax0Dx20-x7E] # ASCII
- | [xC2-xDF][x80-xBF] # non-overlong 2-byte
- | xE0[xA0-xBF][x80-xBF] # excluding overlongs
- | [xE1-xECxEExEF][x80-xBF]{2} # straight 3-byte
- | xED[x80-x9F][x80-xBF] # excluding surrogates
- | x80-xBF]{3} # planes 4-15
- | xF4[x80-x8F][x80-xBF]{2} # plane 16
- )*$%xs', $string);
- }
- ?>
-
Copy code
Instructions:
The accuracy is basically the same as mb_detect_encoding, right or wrong. But it is basically enough for daily development. I hope everyone likes it.
|