removing doctype while saving domdocument

KoolKabin picture KoolKabin · Mar 29, 2012 · Viewed 8.2k times · Source

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?

Answer

Alix Axel picture Alix Axel · Apr 5, 2012

Same as @KoolKabin answer, but a little shorter:

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