Php judge string encoding function _ PHP Tutorial

Source: Internet
Author: User
Php judges string encoding functions. Character encoding is something we often use. in particular, I want to determine the encoding of character input or submitted by the user for effective processing, next, I will judge the large character encoding that we often use. in particular, I want to determine the encoding of the characters entered or submitted by the user for effective processing, next I will introduce you to php's function for judging string encoding.

Mb_detect_encoding () ($ str );

The code is as follows:


// Determine the encoding of a string
If ($ tag = mb_convert_encoding ($ tag, "GB2312", "UTF-8"), "UTF-8", "GB2312 ")){

}
Else {// if it is gb2312, it will be converted to utf8
$ Tag = mb_convert_encoding ($ tag, 'utf-8', 'gb2312 ');
}

The function can detect encoding. however, to use this function, you must enable the extension of php extension = php_mbstring.dll.

Will php. ini have better functions to check string encoding when configuring folder permissions? yes.

Determines whether the string is UTF-8 encoded

The code is as follows:

/**
+ ----------------------------------------------------------
* Check whether the string is UTF-8 encoded.
+ ----------------------------------------------------------
* @ Param string $ string
+ ----------------------------------------------------------
* @ Return Boolean
+ ----------------------------------------------------------
*/
Function is_utf8 ($ string)
{
Return preg_match ('% ^ (? :
[X09x0Ax0Dx20-x7E] # ASCII
| [XC2-xDF] [x80-xBF] # non-overlong 2-byte
| XE0 [xA0-xBF] [x80-xBF] # excluding overlongs
| [XE1-xECxEExEF] [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 );
}

Check whether GB2312 or UTF-8

The code is as follows:

Function is_gb2312 ($ str)
{
For ($ I = 0; $ I $ V = ord ($ str [$ I]);
If ($ v & gt; 127 ){
If ($ v> = 228) & ($ v <= 233 ))
{
If ($ I + 2) >=( strlen ($ str)-1) return true; // not enough

Characters
$ V1 = ord ($ str [$ I + 1]);
$ V2 = ord ($ str [$ I + 2]);
If ($ v1> = 128) & ($ v1 <= 191) & ($ v2> = 128) & ($ v2 <= 191) // utf

Encoding
Return false;
Else
Return true;
}
}
}
Return true;
}

Some friends say they can use the mb_check_encoding function to check the information. if you have not tested this function, you can test it on your own.

...

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.