当前位置:Gxlcms > PHP教程 > 使用PHPXPath采集的时候,如何保留nodeValue里的html符号

使用PHPXPath采集的时候,如何保留nodeValue里的html符号

时间:2021-07-01 10:21:17 帮助过:8人阅读

代码如下:

$html = <<


    
    Test



  

abcdefghijklmn
opqrstuvwxyz

EOF; // create document object model $dom = new DOMDocument(); // load html into document object model @$dom->loadHTML($html); // create domxpath instance $xPath = new DOMXPath($dom); // get all elements with a particular id and then loop through and print the href attribute $elements = $xPath->query('//*[@id="content"]/p/span'); $content = $elements->item(0)->nodeValue; echo $content;

内容里的
会被去除,使用什么操作比如有没有$e->innerHtml之类的,可以保留html标签。

8.18 更新:

$html = <<


    
    Test



  

abcdefghijklmn
opqrstuvwxyz

EOF; // create document object model $dom = new DOMDocument(); // load html into document object model @$dom->loadHTML($html); // create domxpath instance $xPath = new DOMXPath($dom); // get all elements with a particular id and then loop through and print the href attribute $elements = $xPath->query('//*[@id="content"]/p/span'); $nodeName = $elements->item(0)->nodeName; // $content = $elements->item(0)->nodeValue; $content = $dom->saveXml($elements->item(0)); $content = $dom->saveHtml($elements->item(0)); $content = preg_replace(array("#^<{$nodeName}.*>#isU", "#$#isU"), array('', ''), $content); echo $content;

回复内容:

代码如下:

$html = <<


    
    Test



  

abcdefghijklmn
opqrstuvwxyz

EOF; // create document object model $dom = new DOMDocument(); // load html into document object model @$dom->loadHTML($html); // create domxpath instance $xPath = new DOMXPath($dom); // get all elements with a particular id and then loop through and print the href attribute $elements = $xPath->query('//*[@id="content"]/p/span'); $content = $elements->item(0)->nodeValue; echo $content;

内容里的
会被去除,使用什么操作比如有没有$e->innerHtml之类的,可以保留html标签。

8.18 更新:

$html = <<


    
    Test



  

abcdefghijklmn
opqrstuvwxyz

EOF; // create document object model $dom = new DOMDocument(); // load html into document object model @$dom->loadHTML($html); // create domxpath instance $xPath = new DOMXPath($dom); // get all elements with a particular id and then loop through and print the href attribute $elements = $xPath->query('//*[@id="content"]/p/span'); $nodeName = $elements->item(0)->nodeName; // $content = $elements->item(0)->nodeValue; $content = $dom->saveXml($elements->item(0)); $content = $dom->saveHtml($elements->item(0)); $content = preg_replace(array("#^<{$nodeName}.*>#isU", "#$#isU"), array('', ''), $content); echo $content;

自己找到了办法。。。

$content = $elements->item(0)->nodeValue;

// >> 改成 >>

$content = $dom->saveXml($elements->item(0));

人气教程排行