Saving HTML from DOMDocument Without HTML Wrapper
The task at hand is to extract HTML content from a DOMDocument object without it being wrapped in unnecessary HTML tags. The concern is that when using saveXML, it adds XML, HTML, body, and p tag wrappers.
As PHP has advanced to version 5.4 and Libxml to 2.6, a solution has emerged. The loadHTML function now accepts an $option parameter that controls how the content is parsed.
To solve this problem, we can load the HTML with the following options:
$html->loadHTML($content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
These options disable the automatic addition of implied html/body elements and prevent a default doctype from being added. This configuration ensures that when saveHTML is called, the output will not include the problematic wrappers.
LIBXML_HTML_NOIMPLIED: Turns off the automatic adding of implied html/body elements. LIBXML_HTML_NODEFDTD: Prevents a default doctype from being added when one is not found.
For comprehensive documentation on Libxml parameters, refer to: [Libxml Parameters Documentation](https://www.php.net/manual/en/libxml.constants.php#libxmlconstants.constants.html)
It's noteworthy that while the loadHTML documentation mentions that Libxml 2.6 is required, LIBXML_HTML_NODEFDTD is only available in Libxml 2.7.8 and LIBXML_HTML_NOIMPLIED is available in Libxml 2.7.7.
The above is the detailed content of How to Save HTML from DOMDocument Without Unnecessary Wrapper Tags?. For more information, please follow other related articles on the PHP Chinese website!