When using PHP to handle strings, we often encounter the problem of character encoding conversion, have you ever encountered a ICONV conversion failure?
Found the problem, the search on the Internet, only to find Iconv original bug, encountered some rare words will not be able to convert, of course, the configuration of the second parameter, you can slightly compensate for the default defects, not to be able to convert is truncated, use the following
Iconv ("Utf-8″," GB2312//IGNORE ", $data) ;
In this way, when the conversion fails, it will ignore the failure, continue to convert the following content, this is a solution to the problem, but in order to ensure the success of the conversion, we can use another conversion function (mb_convert_encoding), the data online said that the function is not very high efficiency, In addition, this function can also omit the third parameter, automatically identify the content encoding, but it is best not to use, affect efficiency, also need to pay attention to, mb_convert_encoding and iconv parameter order is not the same, must pay attention to.
A simple usage of two functions:
Iconv
String iconv (string $in _charset , string $out _charset , string $str )
First parameter: Encoding of content origin
Second parameter: Target encoding
Third argument: the string to go
function return string
<? PHP $instr = ' Test '; // GBK Turn UTF-8 $outstr Iconv (' GBK ', 'utf-8′, $instr);? >
Mb_convert_encodingstring mb_convert_encoding (String $str, String $to _encoding [, Mixed $from _encoding]) First argument: the string to be processed Second parameter: Target encoding third parameter: Content original encoding
<? PHP $instr = ' Test '; // GBK Turn UTF-8 $outstr = mb_convert_encoding ($instr, ' UTF-8 ', ' GBK ',); >
transcoding problem when using mb_convert_encoding comparison insurance.