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

不言
Release: 2023-03-23 22:32:01
Original
2436 people have browsed it

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);
Copy after login

<br/><br/>

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/>
Copy after login

<br/>

To solve this problem, you can use the following code to achieve it:

<br/>

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

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:

<br/>

$zhuan=iconv("UTF-8","GB2312//IGNORE",$data);
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template