17

I am parsing and fetching html documents to DOMDocument. Those documents are child forms that will be displayed inside another page. While saving parsed DOMDocuments, it automatically adds doctype, html, head and body tags. since i am working on child forms i would like to remove all those and save only the child tags of form.

How can i skip automatic generation of html, head, body and other tags while saving domdocument?

KoolKabin
  • 17,157
  • 35
  • 107
  • 145
  • Possible duplicate of [How to saveHTML of DOMDocument without HTML wrapper?](https://stackoverflow.com/questions/4879946/how-to-savehtml-of-domdocument-without-html-wrapper) – miken32 Nov 02 '18 at 02:07

3 Answers3

31

Same as @KoolKabin answer, but a little shorter:

return preg_replace('~<(?:!DOCTYPE|/?(?:html|body))[^>]*>\s*~i', '', $dom->saveHTML());
Alix Axel
  • 151,645
  • 95
  • 393
  • 500
20

As of PHP 5.4 and Libxml 2.6, there is currently simpler approach: when you load html as this

$html->loadHTML($content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); 

in the output, there will be no doctype, html or body tags. source

Anil Chaudhari
  • 801
  • 14
  • 30
  • Please, take a look at this comment on a similar question https://stackoverflow.com/questions/4879946/how-to-savehtml-of-domdocument-without-html-wrapper#comment103143168_22490902 – October Eleven Nov 09 '22 at 13:51
16

Got it myself after reading through hundreds of links. hope it helps other guys too...

return preg_replace('/^<!DOCTYPE.+?>/', '', str_replace( array('<html>', '</html>', '<body>', '</body>'), array('', '', '', ''), $objDOM->saveHTML()));

Detail on: http://www.php.net/manual/en/domdocument.savehtml.php

KoolKabin
  • 17,157
  • 35
  • 107
  • 145