Use iconv () function to convert character encoding

Source: Internet
Author: User
The iconv function library of php can be used to convert character sets. It is an indispensable basic function library in php programming. However, an error occurs when iconv is used to convert some special characters, such as-To gb2312. First look at the usage of this function: the simplest application, replace gb2312 into UTF-8: $ texticonv (GB2312, UTF-8, $ text); in the use of $ text

The iconv function library of php can be used to convert character sets. It is an indispensable basic function library in php programming. However, an error occurs when iconv is used to convert some special characters, such as-To gb2312. First look at the usage of this function: the simplest application, replace gb2312 into UTF-8: $ text = iconv (GB2312, UTF-8, $ text); in the use of $ text

The iconv function library of php can be used to convert character sets. It is an indispensable basic function library in php programming.

However, an error occurs when iconv is used to convert some special characters, such as "-" To gb2312.

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

The simplest application is to replace gb2312 with UTF-8:
$ Text = iconv ("GB2312", "UTF-8", $ text );

In the process of $ text = iconv ("UTF-8", "GB2312", $ text), if you encounter some special characters, such as: "-", in the English name ". "and other characters, the conversion will be broken. The text after these characters cannot be converted.

To solve this problem, use the following code:
$ Text = iconv ("UTF-8", "GBK", $ text );

You are not mistaken. It is so simple that you can write GBK instead of gb2312.

Another method is to add // IGNORE to the second parameter to IGNORE the error, as shown below:
Iconv ("UTF-8", "GB2312 // IGNORE", $ data );

The first method (GBK instead of gb2312) is better.

Iconv () description in the php manual:

?iconv?(PHP 4 >= 4.0.5, PHP 5)?iconv – Convert string to requested character encoding?Description?string iconv ( string in_charset, string out_charset, string str )?Performs a character set conversion on the string str from in_charset to out_charset. Returns the converted string or FALSE on failure.?If you append the string //TRANSLIT to out_charset transliteration is activated. This means that when a character can't be 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 be represented in the target charset are silently discarded. Otherwise, str is cut from the first illegal character.

When using this function for string encoding conversion, note that if UTF-8 is converted to gb2312, the string may be truncated. You can solve this problem by using the following methods:
$ Str = iconv ('utf-8', "gb2312 // Transcoder", file_get_contents ($ filepath ));

That is, add the red part in the second parameter to indicate that if the character matching the source encoding cannot be found in the target encoding, a similar character will be selected for conversion. You can also use the: // IGNORE parameter to IGNORE characters that cannot be converted.

Ignore indicates that the conversion error is ignored. Without the ignore parameter, all strings after this character cannot be saved.

Iconv is not the default function of php, but also the default installed module. Installation is required.

For Windows + php, you can modify php. INI file, remove ";" before extension = php_iconv.dll, and copy the iconv under your original php installation file. dll to your winnt/system32 (If your dll points to this directory ). In linux, you can add one more-with-iconv in configure by using static installation. phpinfo can see the iconv items. (Linux7.3 + Apache4.06 + php4.3.2 ).
Mb_convert_encoding and iconv Functions

Mb_convert_encoding is used to convert the encoding. I have never understood the concept of program encoding, but now it seems a bit open. However, the English language generally does not have Encoding Problems. Only Chinese data can have this problem. For example, when you use Zend Studio or Editplus to write a program, you use gbk encoding. If the data needs to be imported into the database and the database is encoded as utf8, You need to encode and convert the data, otherwise, the database will become garbled.

Do a GBK To UTF-8:

 

Another GB2312 To Big5:

 

However, to use the above functions, you need to install but enable mbstring extension Library first.

String mb_convert_encoding (string str, string to_encoding [, mixed from_encoding]) must first enable mbstring extension library, in php. ini will; extension = php_mbstring.dll before; remove mb_convert_encoding can specify multiple input encodings, it will automatically identify according to the content, but the execution efficiency is much worse than iconv;

String iconv (string in_charset, string out_charset, string str) Note: In addition to specifying the encoding to be converted, you can also add two suffixes: // Transcoder and // IGNORE. // Transcoder automatically converts a character that cannot be directly converted into one or more similar characters, and // IGNORE ignores the characters that cannot be converted, by default, the result is truncated from the first invalid character.

Generally, iconv is used. The mb_convert_encoding function is used only when the encoding of the original encoding cannot be determined or the iconv cannot be normally displayed after conversion.

$content = iconv("GBK", "UTF-8″, $content);$content = mb_convert_encoding($content, "UTF-8″, "GBK");

Original article address: The iconv () function is used to convert character encoding. Thank you for sharing it with the original author.

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.