How to convert php to encoding

藏色散人
Release: 2023-03-07 09:52:01
Original
3382 people have browsed it

How to implement PHP encoding conversion: 1. Use the iconv function to convert the target encoding, with syntax such as "iconv('GBK','UTF-8′,$instr);"; 2. Convert the encoding through the mb_convert_encoding function .

How to convert php to encoding

Recommended: "PHP Video Tutorial"

When using PHP to process strings, we often encounter The problem of character encoding conversion, have you ever encountered iconv conversion failure?

When I discovered the problem, I searched online and found that iconv had a bug. It would not be able to convert some rare words. Of course, when configuring the second parameter, you can make up for the default defects a little, so as not to cause problems. If it cannot be converted, it is truncated. The usage is as follows

iconv(“UTF-8″,”GB2312//IGNORE”,$data)  ;
Copy after login

In this way, when the conversion of rare words fails, it will ignore the failure and continue to convert the following content. This is a way to solve the problem, but in order to ensure the success rate of conversion , we can use another conversion function (mb_convert_encoding). According to the information online, this function is not very efficient. In addition, this function can also omit the third parameter and automatically identify the content encoding. However, it is best not to use it, as it affects efficiency and also It should be noted that the order of mb_convert_encoding and iconv parameters is different, so be sure to pay attention.

Attached are the simple usage of two functions:

iconv

string iconv ( string $in_charset , string $out_charset , string $str )
Copy after login

The first parameter: the original encoding of the content

Two parameters: target encoding

The third parameter: the string to be converted

The function returns the string

Copy after login

mb_convert_encoding

string mb_convert_encoding ( string $str , string $to_encoding [, mixed $from_encoding ] )
Copy after login

The first parameter: the string to be processed

The second parameter: the target encoding

The third parameter: the original encoding of the content

Copy after login

Personal suggestion When it comes to transcoding problems, it is safer to use mb_convert_encoding.

The above is the detailed content of How to convert php to encoding. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!