【发布时间】:2012-04-08 22:46:28
【问题描述】:
我正在尝试制作一个 html 解析器,但是当我加载 html 时,我会收到这样的警告
警告:DOMDocument::loadHTML() [domdocument.loadhtml]: Invalid char in CDATA 0x1C in Entity, line: 1302
这是我使用的代码
class Parser
{
public $url=null;
public $html=null;
public $tidy=null;
public $head=null;
public $head_xpath=null;
function __construct($url){
$this->url=$url;
$this->html=file_get_contents($this->url);
$this->tidy=tidy_parse_string($this->html);
$this->head=new DOMDocument();
$this->head->loadHTML($this->tidy->head());
$this->head_xpath= new DOMXPath($this->head);
}
}
$x=new Parser("http://www.guardian.co.uk/politics/2012/mar/24/vince-cable-coalition-banking-row");
我四处寻找并找到了 LIBXML_NOCDATA 常量,但我不知道如何设置它。 那我怎么能完全忽略 CDATA?
【问题讨论】:
-
而且我想在内容中盲目替换它不是一种选择,因为它可能在某些文章中?
-
你想从这个 HTML 页面解析什么?
-
@Artjom Kurapov 我尝试用 str_replace 替换 cdata 标签,但它仍然给出警告,我不能总是确定我删除了它们,因为有些人写它们像 //(这里有很多空格)
-
我放这个页面是因为里面有cdata。我正在寻找一般的元标记
-
你能从 xml 中发布违规行吗?如果它是格式正确的 cdata,则不应抛出该错误。看起来你可能有字符编码问题。