The Perfect 2 php detection string is UTF-8 encoded function sharing _php instance

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.