douli4337 2012-03-24 19:58
浏览 71

如何忽略CDATA标签?

I'm trying to make an html parser, but when I load the html I get warnings like this

Warning: DOMDocument::loadHTML() [domdocument.loadhtml]: Invalid char in CDATA 0x1C in Entity, line: 1302

Here is the code I use

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

I searched around and found the LIBXML_NOCDATA constant, but I don't know how to set it. So how could i completely ignore CDATA?

  • 写回答

1条回答 默认 最新

  • douguanci9158 2012-03-27 11:25
    关注
    $this->html = preg_replace('~//\s*?<!\[CDATA\[\s*|\s*//\]\]>~', '', $this->html);
    

    should work but haven't really tested it.

    评论

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统