PHP解析无效的HTML [英] PHP parsing invalid html

查看:106
本文介绍了PHP解析无效的HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i'm trying to parse some html that is not on my server

    $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.那么,即使无效,我该如何解析呢?

but php returns an error something like ID his_id already defined in http://www.some-site.org/page.aspx, line: 33. I think that is because DOMDocument is dealing with invalid html. So, how can i parse it even though is invalid?

推荐答案

您应运行 HTML对其进行整理,以便在解析之前对其进行清理.

You should run HTML Tidy on it to clean it up before parsing it.

$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);

请参见此选项列表.

这篇关于PHP解析无效的HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆