使用iconv()函数即可实现
把UTF-8转换成gb2312
$content = iconv("utf-8","gb2312",$content);
这样往往不行,会出现错误
$content = iconv("utf-8","gb2312//IGNORE",$content);
加上//IGNORE才可以,//IGNORE可以使iconv()忽略错误继续执行
同理gb2312转成UTF-8也是这样,其他编码转换也是如此
iconv():
string iconv ( string $in_charset , string $out_charset , string $str )
第一个参数:内容原的编码
第二个参数:目标编码
第三个参数:要转的字符串
函数返回字符串
<?php
$instr = ‘测试’;
// GBK转UTF-8
$outstr = iconv(‘GBK’,'UTF-8′,$instr);
?>
相同作用函数:
mb_convert_encoding():
string mb_convert_encoding ( string $str , string $to_encoding [, mixed $from_encoding ] )
第一个参数:要处理的字符串
第二个参数:目标编码
第三个参数:内容原编码
<?php
$instr='测试';
//GBK转UTF-8
$outstr =mb_convert_encoding($instr,'utf-8','GBK');
echo $outstr;