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 that it will not be impossible to convert. Truncate, usage is as follows
iconv(“UTF-8″,”GB2312//IGNORE”,$data) ;
When encountering a rare word conversion failure, 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, which affects the efficiency. You also need to pay attention to the order of the mb_convert_encoding and iconv parameters. Same, be sure to pay attention.
Attached are the simple usages of two functions:
string iconv ( string $in_charset , string $out_charset , string $str )
The first parameter: the encoding of the original content
Second parameter: target encoding
The third parameter: the string to be converted
Function returns string
$instr = ‘test’;
// GBK to UTF-8
$outstr = iconv(‘GBK’,’UTF-8′,$instr);
?>
<?php $instr = '测试'; // GBK转UTF-8 $outstr = mb_convert_encoding($instr,'UTF-8','GBK',);
?>
Personally, it is recommended to use mb_convert_encoding to be safer when encountering transcoding problems.