php用preg_match_all采集html网页内容时容易出现中文乱码,总结为以下几种解决方案。
1.如果只是简单的编码问题,将当前php文件改成utf8编码试试。
2.如果还是不行,就用iconv进行转码
$content = iconv(”GBK”, “UTF-8″, $content);
$content = mb_convert_encoding($content, “UTF-8″, “GBK”);
preg_match_all("|
(.*)[li>]+>|U",$content, $out, PREG_SET_ORDER);echo $out[0][0];
$file = file_get_contents("http://blog.xbmu.info");//远程抓取
preg_match_all();//提取信息
//根据需要选择转码:
$str2 = iconv("utf-8", "gb2312//IGNORE",$str2);//把utf转gb2312
$str2 = iconv("gb2312", "utf-8//IGNORE",$str2);//把gb2312转utf
3.当遇到无法确定原编码是何种编码,或者iconv转化后无法正常显示时才用mb_convert_encoding 函数或者mb_ereg_*系列函数,详情参见PHP的Manual。