当前位置:Gxlcms > PHP教程 > -phpQuery采集出现乱码

-phpQuery采集出现乱码

时间:2021-07-01 10:21:17 帮助过:5人阅读

在线等-phpQuery采集出现乱码
关于采集电影天堂出现乱码,求高手帮处理。。。。。。。。

header("Content-type: text/html; charset=utf-8");
set_time_limit(0);
require_once("../phpQuery/phpQuery.php");

function shootCodeBug($data){
$data=mb_convert_encoding($data,'ISO-8859-1','gb2312');
return mb_convert_encoding($data,'utf-8','GBK');
}

phpQuery::newDocumentHTML(shootCodeBug('http://www.dytt8.net/'));
phpQuery::$defaultCharset="gb2312";
$content = pq("#menu")->html();
phpQuery::$documents=array(); //清空缓存
------解决思路----------------------
这是你的使用方法不当造成的
phpQuery 是利用 DOMDocument 做 HTML 解析的
而 DOMDocument::loadHTML 方法在载入 HTML 数据时,会根据文档中的 标记来识别文档的字符集。
如果文档中没有这个标记,则取默认字符集 ISO-8859-1

因此,如果待处理的内容没有字符集声明标记时,应自己附加上去
设 $html 为待分析的内容,则
if(! preg_match('/]+charset=/i', $html)) {
$charset = mb_check_encoding($html, 'utf-8') ? 'utf-8' : 'gbk';
$html = sprintf('%s', $charset, $html);
}
再给 phpQuery 处理就没有问题了

当然,你也可以整体的转换成 utf-8 编码的

人气教程排行