PHP Iconv () character encoding conversion problem _php Tutorial

Source: Internet
Author: User
Tags translit
In PHP function library has a function: Iconv (), Iconv function library can complete the conversion between various character sets, is an indispensable base Function library in PHP programming.

Recently in a thief program, need to use the ICONV function to fetch the UTF-8 encoded page into gb2312, found that only with the ICONV function to fetch the data of a transcoding data will be less for no reason. Let me depressed for a while, go online a check information to know this is a iconv function of a bug. Iconv error when converting character "-" to gb2312.

Let's take a look at the usage of this function.

The simplest application, replace the gb2312 into Utf-8:

$text =iconv ("GB2312", "UTF-8", $text);

In the process of using $text=iconv ("UTF-8", "GB2312", $text), if some special characters are encountered, such as: "-", "." in English name. And so on character, the conversion is broken off. The text after these characters is not able to continue the conversion.

The following code can be implemented for this problem:

$text =iconv ("UTF-8", "GBK", $text);

You have not read wrong, it is so simple, do not use gb2312, and write GBK, just can.

There is another way, the second argument, plus//ignore, ignores the error, as follows:

Iconv ("UTF-8", "Gb2312//ignore", $data);

There is no specific comparison between the two methods, the feeling of the first (GBK instead of gb2312) method is better.

Instructions for Iconv () in the PHP manual:

Iconv (PHP 4 >= 4.0.5, PHP 5) Iconv–convert string to requested character encodingdescriptionstring Iconv (string in_ CharSet, String out_charset, String str) performs a character set conversion on the string str from In_charset to Out_char Set. Returns the converted string or FALSE on failure. If you append the string//translit to Out_charset transliteration is activated.  This means if a character can ' t is represented in the target charset, it can be approximated through one or several Similarly looking characters. If you append the string//ignore, characters that cannot is represented in the target charset is silently discarded. Otherwise, str is cut from the first illegal character.

When using this function for string encoding conversions, it is important to note that if you convert Utf-8 to gb2312, a string truncation can occur. This can be resolved using the following methods:

$str =iconv (' Utf-8 ', "Gb2312//translit", file_get_contents ($filepath));

That is, in the second parameter, add the Red Word section, meaning: If you cannot find a character that matches the source encoding in the target encoding, a similar character is selected for conversion. This can also be used here://ignore This parameter, which means that characters that cannot be converted are ignored.

Ignore means ignoring errors at the time of conversion, and if there is no ignore argument, all strings after that character cannot be saved.

Iconv is not the default function for PHP, it is also the default installed module. Need to be installed to use.

If it is windows2000+php, you can modify the php.ini file, will extension=php_iconv.dll before the ";" Remove and copy the Iconv.dll under your original PHP installation file to your Winnt/system32 (if your DLL is pointing to this directory). In the Linux environment, the static installation of the way, in the configure when adding a--with-iconv on it, phpinfo see iconv items. (linux7.3+apache4.06+php4.3.2).

Introduction to Mb_convert_encoding and ICONV functions

Mb_convert_encoding This function is used to convert the encoding. It turns out that the concept of program coding is not understood, but it seems to be a bit enlightened now. However, there is generally no coding problem in English, only the Chinese data will have this problem. For example, when you write a program with Zend Studio or editplus, using GBK encoding, if the data needs to enter the database, and the database encoding is UTF8, then the data will be encoded conversion, or into the database will become garbled.

Do a GBK to UTF-8:

 
  

One more GB2312 to Big5:

 
  

However, to use the above function requires installation but first enable the mbstring extension library.

String mb_convert_encoding (String str, string to_encoding [, mixed from_encoding]) need to enable the Mbstring extension library first, in php.ini; Extension=php_mbstring.dll in front of; Remove mb_convert_encoding can specify a variety of input encoding, it will automatically identify according to the content, but the execution efficiency is much worse than the iconv;

String Iconv (String in_charset, String out_charset, String str) Note: The second parameter, in addition to the encoding you can specify to convert to, can also add two suffixes://translit and//ignor E, where//translit automatically converts characters that cannot be converted directly into one or more approximate characters,//ignore ignores characters that cannot be converted, and the default effect is to truncate from the first illegal character.

In general, with Iconv, only use the Mb_convert_encoding function if you encounter an inability to determine what encoding the original encoding is, or if the Iconv conversion fails to display properly.

$content = Iconv ("GBK", "Utf-8″, $content); $content = mb_convert_encoding ($content," Utf-8″, "GBK");

http://www.bkjia.com/PHPjc/752405.html www.bkjia.com true http://www.bkjia.com/PHPjc/752405.html techarticle in PHP function library has a function: Iconv (), Iconv function library can complete the conversion between various character sets, is an indispensable base Function library in PHP programming. Recently in the process of doing a thief, need ...

  • 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.