简体   繁体   English

没有DTD,head和body标签的PHP DOMDocument?

[英]PHP DOMDocument without the DTD, head, and body tags?

Is it possible to use the DOMDocument class and not allow it to add doc type declarations, head, and body tags? 是否可以使用DOMDocument类并且不允许它添加doc类型声明,head和body标签? I am writing my current bit of code for a server side include, and it is being rendered on an already well formed page. 我正在为服务器端include编写我当前的代码,并将其呈现在一个格式正确的页面上。 I don't need additional tags. 我不需要其他标签。

@Wrikken所说的,或者,对于PHP <5.3.6,只需使用正则表达式即可:

$html = preg_replace('~<(?:!DOCTYPE|/?(?:html|body))[^>]*>\s*~i', '', $dom->saveHTML());

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. 从PHP 5.3.6开始,您可以在echo $DOMDocument->saveHTML($the_node_you_want_to_show)使用一个节点,在此之前,我滥用->saveXML()进行了较小的修复。 You must however have 1 surrounding included node (eg output is <div>...somecontent and nodex....</div> , or loop through the nodes children if you don't want have 1 surrounding tag; 但是,您必须有1个周围包含的节点(例如,输出为<div>...somecontent and nodex....</div> ;如果您不希望有1个周围标签,则循环遍历节点子节点;

$html = '';
foreach($rootnode->childNodes as $node){
    $html .= $rootnode->ownerdocument->saveHTML($node);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM