How to use mb_detect_encoding function in PHP

Source: Internet
Author: User
This article mainly introduces the use of mb_detect_encoding functions in PHP, including the conversion and judgment of string encoding, and the resolution of the call to undefined function mb_detect_encoding () error. A friend you need can refer to the following

PHP can use the mb_detect_encoding () function to determine what encoding a string is.

When using the Mb_detect_encoding function in PHP to encode recognition, many people have encountered the problem of identification coding errors, such as to GB2312 and UTF-8, or UTF-8 and GBK (here is mainly for cp936 judgment), Online is due to the short-term character mb_detect_encoding will appear false.

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 detect whether the encoding of a string is UTF-8, and then convert to GBK.
But when $keytitle = "%d0%be%c6%ac"; The test results are UTF-8. This bug is not really a bug, writing programs should not be too dependent on mb_detect_encoding, when the string is shorter, the detection result is very likely to deviate.
Workaround, the code is as follows:

$encode = mb_detect_encoding ($keytitle, Array ("ASCII", "GB2312", "GBK", "UTF-8");

The three parameters are: The detected input variables, the encoding method of the detection sequence (once true, followed by auto-ignore), the strict mode of encoding detection of the order of adjustment, the maximum probability is placed in front, so as to reduce the chance of being wrongly converted.
Generally to first row gb2312, when there are GBK and UTF-8, you need to put the usual arrangement to the front.

Conversion and judgment 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 form of encoding at all. There are two library functions that can support the conversion of the encoding, usually can think of is the ICONV function, use is also very cool:
Iconv (' GBK ', ' utf-8//ignore ', ' Test string '); Converts a string from GBK encoding to UTF-8 encoding

However, Iconv can only solve the pre-known encoding situation, if the string encoding is unknown, you need to first probe its encoding, you may use the Mb_string extension library:
Mb_detect_encoding (' Test string ');

But mb_detect_encoding there is a mishap, often appear to judge inaccurate situation. Perhaps this will solve:

Use Iconv to convert and determine if equivalent, inefficient function Is_utf8 ($str) {  if ($str = = = Iconv (' UTF-8 ', ' Utf-8//ignore ', $str)} {    return ' UTF-8 ';  }} Multiple encoding conditions function detect_encoding ($str) {  foreach (' GBK ', ' UTF-8 ') as $v) {    if ($str = = = Iconv ($v, $v. '//ignore ', $str)) {      return $v;    }}  }

After getting the string encoding information in the above way, you can use Iconv or mb_convert_encoding to convert the encoding.

Call to undefined function mb_detect_encoding () Error resolution

Under Windows system:
One, Fatal error:call to undefined Function:iconv () in C:\Program files\appserv\www\...\xxx.php on line 82
PHP has a iconv () function for character encoding conversions.
Also exists in the php.ini file Extension=php_iconv.dll
Such a declaration will ensure that this function is used properly
If there is a semicolon in front of Extension=php_iconv.dll in the php.ini file
The indication is commented out, if there is a semicolon to remove, and restart the server
Run the program again to resolve

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 to my PHP extension directory is: C:\Program files\appserv\php\extensions)
Locate 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 my php.ini file is located: C:\WINDOWS)
3. Open the php.ini file with Notepad, use the shortcut key ctrl+f find Extension=php_mbstring.dll
4, if there is extension=php_mbstring.dll in the php.ini file, remove the semicolon in front of the line extension,
If it doesn't exist, it's in the other extension= ... The next line to join Extension=php_mbstring.dll,
Finally remember to save the php.ini file
5. Restart your Apache server

Just found not copy php_mbstring.dll this file to php.ini directory also line

Under Linux system:

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/whois.mysql.php:46
Ab.cnis exist<br/>php Fatal error:call to undefined function mb_detect_encoding () In/home/xu/web/whois/whois.mai N.php on line 98
Find a variety of information on the Internet after the original is php-mbstring this PHP plug-in is not installed (some students may not be in the php.ini to use this plugin), write down this section of the document to make notes.

First use Yum or apt to install the mbstring.so, using the command: Yum install php-mbstring or apt-get install php-mbstring (Ubuntu user Best Use Apt-cache search Mbstring after the search and then install, the software name may be wrong)

Then modify PHP.ini: Execute Vim/etc/php.ini, add extension= "/usr/lib/php/modules/mbstring.so" content, the following path may be different, According to the mbstring.so storage directory to make the corresponding changes can be. It is generally not necessary to change.

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

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.