现在项目基本都是UTF-8编码的,但是有个别网站还是GBK编码的,比如搜狗。采集到的GBK编码的页面处理会导致解析不了html内容。
处理方法
- 转换页面内容为UTF-8
- 替换页面的头部GBK为UTF-8 这一点很重要
/** * 内容处理 * 把GBK转码为utf-8 * 头部标识编码 gbk替换成utf-8(采集的时候页面转换成了utf-8编码,header头也一定记得替换成utf-8编码,否则编码就会有问题) */ $content = iconv('GBK', "UTF-8//ignore", $content); $content = preg_replace("/gb(k|2312)/i", "utf-8", $content);
之前处理字符串编码问题的方法
function doEncoding($str){ $encode = strtoupper(mb_detect_encoding($str, ["ASCII",'UTF-8',"GB2312","GBK",'BIG5'])); if($encode!='UTF-8'){ $str = mb_convert_encoding($str, 'UTF-8', $encode); } return $str; }
curl 检测响应Content-Type编码是GBK的
/** * 转换gbk编码为utf8 * @param $html * @param $curl_info * @return mixed|string */ private function do_html_to_utf8($html, $curl_info) { if($curl_info && preg_match("/gb(k|2312)/", $curl_info['content_type'], $match) > 0) { $encode = $match[0]; $html = iconv($encode, "utf-8//IGNORE", $html); $html = preg_replace("/gb(k|2312)/", "utf-8", $html); } return $html; }
去掉html页面注释正则
$content= preg_replace('#<!--[^\!\[]*?(?<!\/\/)-->#' , '' ,$content);