欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

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

程序员文章站 2024-02-04 10:06:34
...
代码如下:
$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("#^#isU", "#{$nodeName}>$#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("#^#isU", "#{$nodeName}>$#isU"), array('', ''), $content); echo $content;

自己找到了办法。。。

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

// >> 改成 >>

$content = $dom->saveXml($elements->item(0));
相关标签: php