Home > Backend Development > PHP Tutorial > PHP transcoding function Are you still using iconv? _PHP Tutorial

PHP transcoding function Are you still using iconv? _PHP Tutorial

WBOY
Release: 2016-07-21 14:58:21
Original
847 people have browsed it

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:

iconv

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);

?>

mb_convert_encoding

string mb_convert_encoding ( string $str , string $to_encoding [, mixed $from_encoding ] ) The first parameter: the string to be processed Second parameter: target encoding The third parameter: the original encoding of the content
<?php

$instr = '测试';

// GBK转UTF-8

$outstr = mb_convert_encoding($instr,'UTF-8','GBK',);
Copy after login
?>
Copy after login

Personally, it is recommended to use mb_convert_encoding to be safer when encountering transcoding problems.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/363875.htmlTechArticleWhen using PHP to process strings, we often encounter the problem of character encoding conversion. Have you encountered iconv? Conversion failed? When I discovered the problem, I searched online and found that iconv originally had b...
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