Mb_detect_encoding () ($STR);
| The code is as follows |
Copy Code |
Determine what the string is encoded if ($tag = = mb_convert_encoding (mb_convert_encoding ($tag, "GB2312", "UTF-8"), "UTF-8", "GB2312")) {
} else {//if the gb2312 is converted to UTF8 $tag = mb_convert_encoding ($tag, ' UTF-8 ', ' GB2312 '); } |
function can detect encoding but using this function you must open the Extension=php_mbstring.dll extension of PHP, if you are using space without modification
PHP.ini the permissions of the configuration folder will there be a better function to check the string encoding? Yes, yes.
To determine whether a string is UTF-8 encoded
| The code is as follows |
Copy Code |
| /** +---------------------------------------------------------- * Check whether the string is UTF8 encoded +---------------------------------------------------------- * @param string $string strings +---------------------------------------------------------- * @return Boolean +---------------------------------------------------------- */ 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 | XF0[X90-XBF][X80-XBF]{2} # Planes 1-3 | [Xf1-xf3] [X80-XBF] {3} # planes 4-15 | XF4[X80-X8F][X80-XBF]{2} # Plane 16 ) *$%xs ', $string); } |
Can you check out GB2312 or UTF-8?
| The code is as follows |
Copy Code |
| Function is_gb2312 ($str) { for ($i =0; $i <strlen ($STR); $i + +) { $v = Ord ($str [$i] ); if ($v > 127) { if (($v >= 228) && ($v <= 233)) & nbsp; { if (($i +2) >= (strlen ($str )-1) true; //Not enough Characters $v 1 = ord ($str [$i +1]); $v 2 = Ord ($str [$i +2]); if ($v 1 >= 128) && ($v 1 <=191) && ($v 2 >=128) && ($v 2 <= 191))//UTF Coding return false; Else return true; } } } return true; } |
Some friends say you can use the Mb_check_encoding function to check, this I did not test everyone can test OH.