PHP DOMDocument without the DTD, head, and body tags?

前端 未结 2 579
日久生厌
日久生厌 2021-01-16 11:31

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

2条回答
  •  情话喂你
    2021-01-16 12:21

    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

    ...somecontent and nodex....
    , 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);
    }
    

提交回复
热议问题