没有TDD,head和body标签的PHP DOMDocument? [英] PHP DOMDocument without the DTD, head, and body tags?
问题描述
是否可以使用DOMDocument类并且不允许它添加doc类型声明,head和body标签?我正在为服务器端include编写我当前的代码,并将其呈现在一个格式正确的页面上。我不需要其他标签。
Is it possible to use the DOMDocument class and not allow it to add doc type declarations, head, and body tags? I am writing my current bit of code for a server side include, and it is being rendered on an already well formed page. I don't need additional tags.
推荐答案
从PHP 5.3.6开始,您可以在 echo $ DOMDocument-> saveHTML($ the_node_you_want_to_show)
,在此之前,我滥用了-> saveXML()
并进行了较小的修复。但是,您必须在周围包含1个节点(例如,输出为< div> ... somecontent和nodex ....< / div>
,或循环遍历这些节点孩子,如果你不想有1个周围的标签;
Since PHP 5.3.6, you can use a node in echo $DOMDocument->saveHTML($the_node_you_want_to_show)
, before that, I've abused ->saveXML()
with minor fixes. You must however have 1 surrounding included node (e.g. output is <div>...somecontent and nodex....</div>
, or loop through the nodes children if you don't want have 1 surrounding tag;
$html = '';
foreach($rootnode->childNodes as $node){
$html .= $rootnode->ownerdocument->saveHTML($node);
}
这篇关于没有TDD,head和body标签的PHP DOMDocument?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!