Php automatically obtains the string encoding function mb_detect_encoding.

Source: Internet
Author: User

When using the mb_detect_encoding function in php for encoding recognition, many people have encountered the problem of incorrect recognition encoding, such, or UTF-8 and GBK (here mainly for the cp936 Judgment), said online due to short character is, mb_detect_encoding will be wrong.
For example:
Copy codeThe Code is as follows:
$ Encode = mb_detect_encoding ($ keytitle, array ("ASCII", 'utf-8', "gb2312'," GBK ", 'big5 ′));
If ($ encode = "UTF-8 ″){
$ Keytitle = iconv ("UTF-8", "GBK", $ keytitle );
}

The purpose of this Code is to check whether the encoding of the string is UTF-8, if yes, it is converted to GBK.
However, when $ keytitle = "% D0 % BE % C6 % AC. The detection results are UTF-8. This bug is not actually a bug, when writing the program should not be too dependent on mb_detect_encoding, when the string is short, the possibility of deviation of the detection results is very large.
How can this problem be solved? My solution is:
Copy codeThe Code is as follows:
$ Encode = mb_detect_encoding ($ keytitle, array ('ascii ', 'gb2312', 'gbk', 'utf-8 ');

The three parameters are: the input variables to be detected, the check sequence of the encoding method (if true, they will be automatically ignored later), and the strict mode.
Adjust the encoding detection sequence to minimize the possibility of conversion errors.
Generally, we need to first rank gb2312, when there is GBK and UTF-8, we need to sort the commonly used to the front.

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.