How to use the mb_detect_encoding function in PHP

Source: Internet
Author: User
This article describes how to use the mb_detect_encoding function in PHP, including converting and judging string encoding and solving the Calltoundefinedfunctionmb_detect_encoding () error, you can use the mb_detect_encoding () function in php to determine the encoding of a string.

When using the mb_detect_encoding function in php for encoding recognition, many people have encountered the problem of incorrect recognition encoding, such as with GB2312 and UTF-8, or UTF-8 and GBK (here mainly for the cp936 judgment), said on the Internet due to short-term characters mb_detect_encoding will be wrong.

The 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.
The 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 (which is automatically ignored once the encoding method is true), and the strict mode to adjust the encoding detection sequence, put the highest possibility at the beginning to reduce the chance of conversion by 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.

Conversion and determination of PHP string encoding


The conversion of GBK and UTF-8 encoding is a very disgusting thing, such as json_encode in PHP itself does not support GBK encoding. There are two library functions that support encoding conversion. Generally, the iconv function can be used very well:

Iconv ('gbk', 'utf-8 // IGNORE ', 'Test string'); // converts a string from GBK encoding to UTF-8 encoding

However, iconv can only solve the problem of pre-known encoding. if the string encoding is unknown, you need to first detect its encoding. in this case, the mb_string extension library may be used:

Mb_detect_encoding ('test string ');

However, mb_detect_encoding has a hard injury, which often results in inaccurate judgment. This may solve the problem:


// Use iconv to convert and determine whether it is equivalent. function is_utf8 ($ str) {if ($ str = iconv ('utf-8 ', 'utf-8 // IGNORE ', $ str) {return 'utf-8'; }}// function detect_encoding ($ str) {foreach (array ('gbk', 'utf-8') as $ v) {if ($ str = iconv ($ v, $ v. '// IGNORE', $ str) {return $ v ;}}}

After obtaining the string encoding information, you can use iconv or mb_convert_encoding to convert the encoding information.

Call to undefined function mb_detect_encoding () error solution


In Windows:
I. Fatal error: Call to undefined function: iconv () in C: \ Program Files \ AppServ \ www \... \ xxx. php on line 82
Php has an iconv () function for character encoding conversion.
The extension = php_iconv.dll also exists in the php. ini file.
This declaration ensures the normal use of this function.
If there is a semicolon before extension = php_iconv.dll in the php. ini file
It indicates that it has been commented out. if there is a semicolon, remove it and restart the server.
You can solve this problem by running the program again.

II. Fatal error: Call to undefined function: mb_detect_encoding () in C: \ Program Files \ AppServ \ www \... \ xxx. php on line 1355
1. find the php extension directory (the path of my php extension directory is: C: \ Program Files \ AppServ \ php \ extensions)
Find the php_mbstring.dll file under the extensions directory,
2. copy the php_mbstring.dll file to the directory where the php. ini file is located (the directory where the php. ini file is located: C: \ WINDOWS)
3. open the php. ini file in Notepad and press Ctrl + F to find extension = php_mbstring.dll.
4. if the php. ini file contains extension = php_mbstring.dll, remove the semicolon before the extension line,
If it does not exist, add extension = php_mbstring.dll to the next line of other extension =,
Remember to save the php. ini file.
5. restart your Apache server

I just found that the file php_mbstring.dll is not copied to the directory where php. ini is located.

In Linux:

When the following problem occurs:

PHP 1. {main} ()/home/xu/web/whois/AB .cn. php: 0
PHP 2. base_func-> is_exist ()/home/xu/web/whois/AB .cn. php: 21
PHP 3. strftime ()/home/xu/web/whois. mysql. php: 46
AB. cnis exist
PHP Fatal error: Call to undefined function mb_detect_encoding () in/home/xu/web/whois. main. php on line 98
After searching for various information on the Internet, I realized that the php plug-in php-mbstring was not installed (some may not be installed in php. ini), write this file to take notes.

First install mbstring using yum or apt. so, run the command: yum install php-mbstring or apt-get install php-mbstring (ubuntu users are advised to search for apt-cache search mbstring before installing it. the software name may be incorrect)

Then modify php. ini: run vim/etc/php. ini, add extension = "/usr/lib/php/modules/mbstring. so "content, the following paths may be different, according to mbstring. modify the so directory. Generally, you do not need to change it.

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.