There are many ways to detect string encoding in PHP, the most common is to use the mb_detect_encoding function directly, but there are more advanced ways to use the ASCII value of the character to judge Oh.
Example 1
| The code is as follows |
Copy Code |
function Is_utf8 ($STR) { $c = 0; $b = 0; $bits = 0; $len =strlen ($STR); for ($i =0; $i < $len; $i + +) { $c =ord ($str [$i]); if ($c > 128) { if (($c >= 254)) return false; ElseIf ($c >= 252) $bits = 6; ElseIf ($c >= 248) $bits = 5; ElseIf ($c >=) $bits = 4; ElseIf ($c >= 224) $bits = 3; ElseIf ($c >= 192) $bits = 2; else return false; if (($i + $bits) > $len) return false; while ($bits > 1) { $i + +; $b =ord ($str [$i]); if ($b < | | $b > 191) return false; $bits--; } } } return true; } |
1. Method 1
| The code is as follows |
Copy Code |
function Mb_is_utf8 ($string) { Return mb_detect_encoding () ($string, ' UTF-8 ') = = = ' UTF-8 ';//New discoveries } |
2. Method 2
| The code is as follows |
Copy Code |
function Preg_is_utf8 ($string) { Return Preg_match ('/^.*$/u ', $string) > 0;//preg_match ('/^./u ', $string) } |
http://www.bkjia.com/PHPjc/632796.html www.bkjia.com true http://www.bkjia.com/PHPjc/632796.html techarticle There are many ways to detect string encoding in PHP, the most common is to use the mb_detect_encoding function directly, but there are more advanced ways to use the ASCII value of the character to judge Oh. ...