This article mainly introduces two perfect php functions to check whether the string is UTF-8 encoded. it is a powerful and concise one, for more information, see transcoding functions, such as iconv () and mb_convert_encoding, when using function transcoding or decoding, we sometimes need to determine whether the current string encoding type is UTF-8 or not, and then perform encoding conversion and other operations. Below is a small series of finishing the current web development on the network usage is relatively high, good php on the UTF-8 coding judgment function, the code is as follows:
Function is_utf8 ($ string) // function 1 {// From http://w3.org/International/questions/qa-forms-utf-8.htmlreturn preg_match ('% ^ (? : [\ X09 \ x0A \ x0D \ x20-\ x7E] # ASCII | [\ xC2-\ xDF] [\ x80-\ xBF] # non-overlong 2-byte | \ xE0 [\ xA0-\ xBF] [\ x80-\ xBF] # excluding overlongs | [\ xE1-\ xEC \ xEE \ xEF] [\ 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);} function mb_is_utf8 ($ string) // function 2 {return mb_detect_encoding ($ string, 'utf-8 ′) === 'utf-8 ′;}
The mb_detect_encoding () function is a built-in function of php used to determine the current string encoding type. this function has three parameters. The first parameter is the string to be determined, the second parameter is the character encoding list for comparison, which can be a string or an array. The third parameter is required.
We hope these two functions will be helpful to the Phper.