简体   繁体   中英

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? 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.

@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. 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;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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