Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

removing doctype while saving domdocument

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?

like image 432
KoolKabin Avatar asked Mar 29 '12 11:03

KoolKabin


2 Answers

Same as @KoolKabin answer, but a little shorter:

return preg_replace('~<(?:!DOCTYPE|/?(?:html|body))[^>]*>\s*~i', '', $dom->saveHTML());
like image 60
Alix Axel Avatar answered Oct 30 '22 14:10

Alix Axel


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

like image 20
Anil Chaudhari Avatar answered Oct 30 '22 12:10

Anil Chaudhari