使用gbk页面编码问题,domxpath

时间:2013-07-24 01:52:25

标签: php dom xpath extract

我正在尝试将下面的链接卷入GBK。我想提取产品和图像的标题。但当我回复文件以测试它是否正常工作时,我不会得到中文字符。我需要使用domxpath提取并在我的网站上显示字符,相同的字符,而不是奇怪的字符。这实际上是如何工作的?

$ch = curl_init("http://item.taobao.com/item.htm?spm=a2106.m874.1000384.41.aG3Kbi&id=20811635147&_u=o1ffj7oi9ad3&scm=1029.newlist-0.1.16&ppath=&sku=");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
$content = curl_exec($ch);
curl_close($ch);


$doc = new DOMDocument();
$searchPage = mb_convert_encoding($content, 'utf-8', "auto");
$doc->loadHTML($searchPage);
echo $doc->saveHTML(); 

2 个答案:

答案 0 :(得分:3)

检查php.ini中的mbstring.language是否设置为GBK,或明确使用

$searchPage = mb_convert_encoding($content, 'utf-8', "gb18030");

答案 1 :(得分:0)

我有同样的问题。解决方案对我有用:

  $str = file_get_contents($url);
  $str = mb_convert_encoding($str,'utf-8', "gb18030");
  $str = str_replace('<head>', '<head><meta HTTP-EQUIV=Content-Type content="text/html;charset=utf-8">', $str);
  $dom = new DOMDocument('1.0');
  @$dom->loadHTML($str);

DOMDocument在html中读取你的编码声明,在头

之后立即将其写入