This article will give you a detailed introduction to the method of using the iconv function to convert string encoding in PHP and the bug analysis of the iconv function. Friends in need may refer to it.
iconv is not the default function of PHP, and it is also a module installed by default. It needs to be installed before it can be used.
If it is windows2000+php, you can modify the php.ini file and remove the ";" before extension=php_iconv.dll. At the same time, you need to copy the iconv.dll in your original php installation file to your winnt/system32
is used as follows:
The code is as follows |
Copy code |
代码如下 |
复制代码 |
$string = "亲爱的朋友欢迎访问胡文芳的博客,希望给您带来一点点的帮助!";
iconv("utf8","gbk",$string)//将字符串string 编码由utf8转变成gbk;
|
$string = "Dear friends, welcome to Hu Wenfang's blog, I hope it can bring you a little help!";
iconv("utf8","gbk",$string)//Convert the string encoding from utf8 to gbk;
|
代码如下 |
复制代码 |
echo $str= '你好,欢迎访问胡文芳的博客,该博客记录一个程序员的成长过程!';
echo '
';
echo iconv('GB2312', 'UTF-8', $str); //将字符串的编码从GB2312转到UTF-8
echo '
';
echo iconv_substr($str, 1, 1, 'UTF-8'); //按字符个数截取而非字节
print_r(iconv_get_encoding()); //得到当前页面编码信息
echo iconv_strlen($str, 'UTF-8'); //得到设定编码的字符串长度
//也有这样用的
$content = iconv("UTF-8","gbk//TRANSLIT",$content);
|
expands as follows:
The code is as follows |
Copy code |
echo $str= 'Hello, welcome to Hu Wenfang's blog, which records the growth process of a programmer!';
echo '
';
echo iconv('GB2312', 'UTF-8', $str); //Convert the string encoding from GB2312 to UTF-8
代码如下 |
复制代码 |
$str="www.bkjia.com,ok!the string is € .我要转换他!⊙●○①⊕◎Θ⊙¤?";
echo '没有任何参数 : ', iconv("utf-8", "gbk", $str)."
";
|
echo '
';
echo iconv_substr($str, 1, 1, 'UTF-8'); //Truncate by the number of characters instead of bytes
print_r(iconv_get_encoding()); //Get the current page encoding information
echo iconv_strlen($str, 'UTF-8'); //Get the string length of the set encoding
//This is also used
$content = iconv("UTF-8","gbk//TRANSLIT",$content);
代码如下 |
复制代码 |
$infocontent=iconv("utf-8","gbk//IGNORE",$infocontent);
或者
$infocontent=iconv("utf-8","gbk//TRANSLIT",$infocontent);
|
|
A small bug in the iconv function
Try converting the following string using iconv to see the effect.
The code is as follows |
Copy code |
$str="www.bkjia.com,ok!the string is € .I want to convert it!⊙●○①⊕◎Θ⊙¤?";
echo 'Without any parameters: ', iconv("utf-8", "gbk", $str)."
";
|
The result returned on my computer is that characters after the first special character cannot be displayed.
iconv has two parameters: TRANSLIT and IGNORE. The respective meanings are TRANSLIT, which means that if the original encoding cannot be translated in the target encoding, then just find a similar closest character or string to replace it. For example, € may be is replaced by EUR; and when IGNORE encounters original characters that cannot be translated by the target character set, it will be skipped and ignored without returning false.
How to use it:
The code is as follows |
Copy code |
$infocontent=iconv("utf-8","gbk//IGNORE",$infocontent);
or
$infocontent=iconv("utf-8","gbk//TRANSLIT",$infocontent);
|
In view of this, I think iconv should provide a default parameter IGNORE to avoid returning an empty string when developers use it improperly.
http://www.bkjia.com/PHPjc/445288.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445288.htmlTechArticleThis article will introduce in detail how to use the iconv function to convert string encoding in php and about iconv function bugs Analysis, friends in need may refer to it. iconv is not the default for php...