Remove parent element, keep all inner children in DOMDocument with saveHTML

前端 未结 5 2156
情歌与酒
情歌与酒 2021-01-04 20:42

I\'m manipulating a short HTML snippet with XPath; when I output the changed snippet back with $doc->saveHTML(), DOCTYPE gets added, and HTML / BODY

5条回答
  •  一向
    一向 (楼主)
    2021-01-04 21:01

    tl;dr

    requires: PHP 5.4.0 and Libxml 2.6.0

    $doc->loadHTML("

    test

    ", LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);

    explanation

    http://php.net/manual/en/domdocument.loadhtml.php "Since PHP 5.4.0 and Libxml 2.6.0, you may also use the options parameter to specify additional Libxml parameters."

    LIBXML_HTML_NOIMPLIED Sets HTML_PARSE_NOIMPLIED flag, which turns off the automatic adding of implied html/body... elements.

    LIBXML_HTML_NODEFDTD Sets HTML_PARSE_NODEFDTD flag, which prevents a default doctype being added when one is not found.

提交回复
热议问题