Home  >  Article  >  Backend Development  >  Use iconv encoding conversion in PHP to solve the problem of Chinese garbled characters_PHP tutorial

Use iconv encoding conversion in PHP to solve the problem of Chinese garbled characters_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:55:331143browse

Using the iconv function to convert the captured utf-8 encoded page into gb2312, I found that only by using the iconv function to transcode the captured data, the data will be less for no reason

echo iconv('GB2312', 'UTF-8', $str); //Convert the string encoding from GB2312 to UTF-8
The code is as follows
 代码如下 复制代码

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

Copy code


echo $str= 'Hello, we sell coffee here!';

echo '
';
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 encoding information of the current pageecho iconv_strlen($str, 'UTF-8'); //Get the string length of the set encoding //This is also used $content = iconv("UTF-8","gbk//TRANSLIT",$content); ?> iconv(“UTF-8″,”GB2312//IGNORE”,$data)
Ignore means to ignore errors during conversion. Without the ignore parameter, all strings following this character cannot be saved. The iconv() function is built-in in php5.
http://www.bkjia.com/PHPjc/632242.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632242.htmlTechArticleUse the iconv function to convert the captured utf-8 encoded page into gb2312. I found that only using iconv Once the function transcodes the captured data, the code will be reduced for no reason as follows...
Statement:
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