当前页面是1.php页面,utf-8编码(在zend studio点右键,更改文件本身为utf-8编码),要去拿一个网站的3.php页面,对方是gb2312的编码(注意是文件本身,而不是里面的charset),我拿到后怎么转换?
<?php
$content = file_get_contents("http://www.XXX.com/3.php");
$content = iconv('gbk','utf-8//ignore',$content) ;
echo $content;
?>
如果上面没有iconv库,需要用一个编码对照表。程序参考了dedecms
其实上面还应该先探测一下是不是gbk编码
<?php
/*
*QQ: 290359552
* conver to Utf8 if
$str is not equals to 'UTF-8'
*/
function
convToUtf8($str)
{
if( mb_detect_encoding($str,"UTF-8,
ISO-8859-1, GBK")!="UTF-8"
)
{
return iconv("gbk","utf-8",$str);
}
else
{
return $str;
}
}
?>