Home >Backend Development >PHP Tutorial >iconv function in PHP to convert encoding, such as UTF-8 to GB2312
This article introduces the iconv function in PHP to convert encoding, such as UTF-8 to GB2312. It has a certain reference value. Now I share it with you. Friends in need can refer to it
Recently, when I was working on the CCB interface, I found that the encoding UTF-8 must be converted into GB2312, so I used this iconv function to convert the encoding.
I found that if I use the iconv function to transcode the captured data, the data will be less for no reason. I was depressed for a while, and after checking the information on the Internet, I found out that this was a bug in the iconv function. iconv will make an error when converting the character "-" to gb2312.
Let’s take a look at the usage of this function.
The simplest application, replace gb2312 with utf-8:
$zhuan=iconv("UTF-8","GB2312",$data);
During use, if you encounter some special characters, such as: "— ", "." and other characters in English names, the conversion is broken. The text after these characters cannot be converted further.
<br/>
To solve this problem, you can use the following code to achieve it:
$zhuan=iconv("UTF-8","GBK",$data);
You read that right, that’s it It's simple, just don't use gb2312 and write it as GBK.
There is another method, the second parameter, add //IGNORE, ignore the error, as follows:
$zhuan=iconv("UTF-8","GB2312//IGNORE",$data);
There is no specific comparison between these two methods. , I feel that the first method (GBK instead of gb2312) is better.
Related recommendations:
How to use the iconv function in php
The above is the detailed content of iconv function in PHP to convert encoding, such as UTF-8 to GB2312. For more information, please follow other related articles on the PHP Chinese website!