9

我正在尝试解析一些不在我的服务器上的 html

    $dom = new DOMDocument();
    $dom->loadHTMLfile("http://www.some-site.org/page.aspx");      
    echo    $dom->getElementById('his_id')->item(0);

但 php 返回类似的错误ID his_id already defined in http://www.some-site.org/page.aspx, line: 33。我认为这是因为 DOMDocument 正在处理无效的 html。那么,即使无效,我该如何解析它?

4

3 回答 3

9

您应该在解析它之前对其运行HTML Tidy以清理它。

$html = file_get_contents('http://www.some-site.org/page.aspx');
$config = array(
  'clean' => 'yes',
  'output-html' => 'yes',
);
$tidy = tidy_parse_string($html, $config, 'utf8');
$tidy->cleanRepair();
$dom = new DOMDocument;
$dom->loadHTML($tidy);

请参阅此选项列表

于 2010-04-24T01:23:36.917 回答
2

看看:libxml_use_internal_errors()

http://php.net/libxml_use_internal_errors

于 2011-04-21T09:21:39.937 回答
0

阅读文档,我看到$dom->strictErrorChecking默认为 TRUE。如果你设置会发生什么$dom->strictErrorChecking = false

于 2010-04-24T01:24:41.613 回答