Explain the use of mb_detect_encoding functions in PHP _php tips

Source: Internet
Author: User
Tags install php

You can use the mb_detect_encoding () function in PHP to determine what the string is encoded in.

When using the Mb_detect_encoding function in PHP for code recognition, many people have encountered problems identifying coding errors, such as GB2312 and UTF-8, or UTF-8 and GBK (mainly for cp936), It is said on the net because the character is short time mb_detect_encoding will appear to misjudge.

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 function of this code is to detect whether the encoding of the string is UTF-8, and the words are converted to GBK.
But when $keytitle = "%d0%be%c6%ac"; The test results are UTF-8. This bug is not really a bug, write the program should not be too dependent on the mb_detect_encoding, when the string is shorter, the detection results of the possibility of deviation.
Workaround, the code is as follows:

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

Three parameters are: the input variables are detected, the sequence of the detection of the encoding (once true, automatically ignored after), strict mode for the sequence of code detection adjustment, the greatest possibility of the first, so as to reduce the chance of being incorrectly converted.
Generally want to first row gb2312, when there are GBK and UTF-8, need to be used to arrange to the front.

Conversion and judgment of PHP string encoding


GBK and UTF-8 encoding conversion is a very disgusting thing, such as PHP in the Json_encode itself does not support the GBK form of coding. There are two library functions that can support coded conversions, which are usually thought of as iconv functions, which are also very cool to use:

Iconv (' GBK ', ' utf-8//ignore ', ' Test string '); Converts a string from GBK encoding to UTF-8 encoding

However, the iconv can only solve the situation in which the encoding is known, and if the string encoding is unknown, you need to probe its encoding first, and you may use the Mb_string extension library:

mb_detect_encoding (' Test string ');

However, there is a mishap in mb_detect_encoding, often appear to judge inaccurate situation. Perhaps this can be solved:


Use Iconv to convert and judge whether equivalence, efficiency is not high
function Is_utf8 ($str) {
  if ($str = = = Iconv (' UTF-8 ', ' Utf-8//ignore ', $str)) {
    ret Urn ' UTF-8 ';
  }
Multiple encoded situations
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 in the above way, we 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 conversion
The same exists in the php.ini file Extension=php_iconv.dll
Such a declaration ensures that this function is used normally
If you have a semicolon in front of the Extension=php_iconv.dll in the php.ini file
The expression 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 () into C:\Program files\appserv\www\...\xxx.php on line 1355
1, find the PHP extension directory (my PHP extension directory path is: C:\Program files\appserv\php\extensions)
Locate the Php_mbstring.dll file under the extensions directory,
2. Copy Php_mbstring.dll This file to the directory where php.ini files are located (my php.ini file is located in the directory: C:\WINDOWS)
3, with Notepad open php.ini file, use shortcut keys 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 does not exist, it is 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

I just found out that I didn't actually copy php_mbstring.dll this file to PHP.ini's directory.

Under Linux system:

When the following problems occur:

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
Look up all kinds of information on the Internet before we know it is php-mbstring this PHP plugin is not installed (some students may not be in php.ini to use this plugin), write down this section of the file for notes.

First use Yum or apt to install mbstring.so, using commands: Yum install php-mbstring or apt-get install php-mbstring (Ubuntu users best Use Apt-cache search Mbstring 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 not be the same, According to the mbstring.so stored in the directory to make the appropriate changes can be. It is generally not necessary to change.

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.