Perfect 2 php detection string is UTF-8 encoded function sharing, string utf-8_PHP tutorial

Source: Internet
Author: User
The perfect two php checks whether the string is a UTF-8 encoded function. The perfect two php checks whether the string is a UTF-8 encoded function. the string UTF-8 is sometimes used in php development, such as The iconv () and mb_convert_encoding () functions, share the test string with UTF-8 encoded functions in the perfect two php scripts.

Transcoding functions such as iconv () and mb_convert_encoding () are sometimes used in php Development. when using function transcoding or decoding, we sometimes need to determine the current string encoding type first, it is better to determine whether it is UTF-8 encoding. If yes, 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.


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

Functions of the MBString library used by PHP
$ E = mb_detect_encoding ($ text, array ('utf-8', 'gbk '));
Switch ($ e ){
Case 'utf-8': // if it is UTF-8 encoded
Break;
Case 'gbk': // for GBK encoding
Break
}

UTF-8 encoding of the output string in php

For example, input "Oh" and output % E5 % 91% B5
This is urlencode...

Transcoding functions, such as iconv () and mb_convert_encoding (), are sometimes used in php development...

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.