我正在尝试制作一个 html 解析器,但是当我加载 html 时,我收到这样的警告
警告:DOMDocument::loadHTML() [domdocument.loadhtml]:实体中 CDATA 0x1C 中的无效字符,行: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 呢?
最佳答案
$this->html = preg_replace('~//\s*?<!\[CDATA\[\s*|\s*//\]\]>~', '', $this->html);
应该可以,但还没有真正测试过。
关于php - 如何忽略 CDATA 标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9855055/