方法获取完整的HTML

方法获取完整的HTML

本文介绍了如何从DOMXPath :: query()方法获取完整的HTML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文档,要从中提取未删除内容的特定div.我这样做:

I have document from which I want to extract specific div with it's untouched content.I do:

$dom = new DOMDocument();
$dom->loadHTML($string);//that's HTML of my document, string

和xpath查询:

$xpath = new DOMXPath($dom);
$xpath_resultset =  $xpath->query("//div[@class='text']");
/*I'm after div class="text"*/

现在我对$xpath_resultset

$my_content = $xpath_resultset->item(0);

我得到的是对象(不是字符串)$ my_content,可以回显或将settype()字符串,但是结果是完全剥离的标记?

what I get is object (not string) $my_content which I can echo or settype() to string, but as result I get is with fully stripped markup?

如何在这里从div class ='text'中获得所有?

What to do to get all from div class='text' here?

推荐答案

只需将节点传递到 DOMDocument::saveHTML 方法:

Just pass the node to the DOMDocument::saveHTML method:

$htmlString = $dom->saveHTML($xpath_resultset->item(0));

这将为您提供该特定DOMNode及其所有子代的字符串表示形式.

This will give you a string representation of that particular DOMNode and all its children.

这篇关于如何从DOMXPath :: query()方法获取完整的HTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 11:16