PHP iconv function to convert the encoding, such as UTF-8 to GB2312

Source: Internet
Author: User
This article introduces the content of PHP iconv function to convert the code, such as UTF-8 to GB2312, has a certain reference value, now share to everyone, the need for friends can refer to

Recently in the construction of the construction Bank interface, it is necessary to convert the encoding UTF-8 to GB2312 so the Iconv function is used to convert the encoding.

found that only using 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:


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



In the process of use, if you encounter some special characters, such as: "-", 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:


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

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:


$zhuan =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.

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.