There are many methods to detect string encoding in php. The most common method is to directly use the mb_detect_encoding function, but a more advanced method is to use the ascii value of the character to judge it.
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 & gt; 128 ){ If ($ c> = 254) return false; Elseif ($ c >=252) $ bits = 6; Elseif ($ c >=248) $ bits = 5; Elseif ($ c >= 240) $ 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 <128 | $ 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'; // newly discovered } |
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) } |