Perfect 2 PHP detection strings are utf-8 encoded function sharing, String utf-8_php tutorial

Source: Internet
Author: User

The Perfect 2 php detection string is utf-8 encoded function sharing, string utf-8


In PHP development sometimes use transcoding functions, such as iconv (), mb_convert_encoding () function, when using the function transcoding or decoding time we sometimes need to determine the current string encoding type, rather than whether it is utf-8 encoding, Yes, then do the encoding conversion and so on. The following is a small compilation of the current Web development in the Internet usage ratio of the high, good PHP UTF-8 encoding judgment function, the code is as follows:

function Is_utf8 ($string)//functions one {//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) *$%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 to determine the current string encoding type, this function has three parameters, the first parameter is to determine the string, the second argument is a comparison of the character encoding list, you can make a string, can also be an array, the third parameter is required.
Hopefully these two functions will help you with the phper you need.


How to use PHP to determine if a string is UTF-8 encoded

PHP using the Mbstring library function
$e =mb_detect_encoding ($text, Array (' UTF-8 ', ' GBK '));
Switch ($e) {
Case ' UTF-8 '://If it is UTF8 encoded
Break
Case ' GBK '://If it is GBK encoded
Break
}

Utf-8 encoding of output string in PHP

such as input "ah", output%e5%91%b5
This is UrlEncode's ...

http://www.bkjia.com/PHPjc/851344.html www.bkjia.com true http://www.bkjia.com/PHPjc/851344.html techarticle The Perfect 2 php detection string is utf-8 encoded function sharing, string utf-8 in PHP development sometimes use transcoding functions, such as iconv (), mb_convert_encoding () function, in the letter ...

  • Related Article

    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.