removing doctype while saving domdocument

杀马特。学长 韩版系。学妹 提交于 2019-12-03 10:31:14

Same as @KoolKabin answer, but a little shorter:

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

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!