In PHP development sometimes use transcoding functions, such as iconv (), mb_convert_encoding () function, in the use of function transcoding or decoding when we sometimes need to first determine the current string encoding type, or whether it is utf-8 encoding, Yes, and then code conversion and other operations. The following is a small collation of the current web development in the online usage rate is higher, good PHP on the UTF-8 encoding of the function, the code is as follows:
function Is_utf8 ($string)//functions one
{
//from http://w3.org/International/questions/qa-forms-utf-8.html
Return 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
*$%xs ', $string);
}
function Mb_is_utf8 ($string)//Functions two
{return
mb_detect_encoding ($string, ' utf-8′ ') = = ' utf-8′;
}
The mb_detect_encoding () function is a built-in function of PHP that is used to determine the current string encoding type, which has three arguments, the first argument is the string to be judged, the second parameter is a list of character encodings that can be compared, a string, or an array, and the third parameter is required.
Hopefully these two functions will be helpful for the phper you need.