Home > php教程 > php手册 > php中iconv编码转换来解决中文乱码的问题

php中iconv编码转换来解决中文乱码的问题

WBOY
Release: 2016-06-13 09:55:55
Original
2054 people have browsed it

用到iconv函数把抓取来过的utf-8编码的页面转成gb2312, 发现只有用iconv函数把抓取过来的数据一转码数据就会无缘无故的少一些

 代码如下 复制代码

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

iconv(“UTF-8″,”GB2312//IGNORE”,$data)
ignore的意思是忽略转换时的错误,如果没有ignore参数,所有该字符后面的字符串都无法被保存。

这个iconv()这个函数,在php5中是内置的.

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template