将dom元素的一部分转换为带有html标签的字符串 [英] convert part of dom element to string with html tags inside of them
本文介绍了将dom元素的一部分转换为带有html标签的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试以下操作,但它只打印一个没有标签的文本。
$ dom = new DOMDocument();
$ dom-> loadHTMLFile('http://www.pixmania-pro.co.uk/gb/uk/08920684/art/packard-bell/easynote-tm89-gu-015uk.html');
$ xpath = new DOMXPath($ dom);
$ elements = xpath-> query('// table');
foreach($ elements as $ element)
echo $ element-> nodeValue;
我想要所有的标签和表格中的内容。有人能帮我吗。这将是一个绿色的帮助。
谢谢。
解决方案
- 创建新的DomDocument实例
- 要保存为HTML的克隆节点(包含所有子节点)
- 导入克隆节点到新的DomDocument实例,并将其附加为一个小孩
- 将新实例另存为html
所以这样的东西:
foreach($ elements as $ element){
$ newdoc = new DOMDocument() ;
$ cloned = $ element-> cloneNode(TRUE);
$ newdoc-> appendChild($ newdoc-> importNode($ cloned,TRUE));
echo $ newdoc-> saveHTML();
}
im in need of converting part of DOM element to string with html tags inside of them.
i tried following but it prints just a text without tags in side.
$dom = new DOMDocument();
$dom->loadHTMLFile('http://www.pixmania-pro.co.uk/gb/uk/08920684/art/packard-bell/easynote-tm89-gu-015uk.html');
$xpath = new DOMXPath($dom);
$elements=xpath->query('//table');
foreach($elements as $element)
echo $element->nodeValue;
i want all the tags as it is and the content inside tables. can some one help me. it'll be a greate help.
thanks.
解决方案
- Create new instance of DomDocument
- Clone node (with all sub nodes) you wish to save as HTML
- Import cloned node to new instance of DomDocument and append it as a child
- Save new instance as html
So something like this:
foreach($elements as $element){
$newdoc = new DOMDocument();
$cloned = $element->cloneNode(TRUE);
$newdoc->appendChild($newdoc->importNode($cloned,TRUE));
echo $newdoc->saveHTML();
}
这篇关于将dom元素的一部分转换为带有html标签的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文