Home > Backend Development > PHP Tutorial > How Can I Save a DOMDocument's HTML Content in PHP Without the Default HTML, Body, and P Tags?

How Can I Save a DOMDocument's HTML Content in PHP Without the Default HTML, Body, and P Tags?

Patricia Arquette
Release: 2024-12-10 15:58:09
Original
593 people have browsed it

How Can I Save a DOMDocument's HTML Content in PHP Without the Default HTML, Body, and P Tags?

Save HTML of DOMDocument Without HTML Wrapper

In your PHP function, you are attempting to save the contents of a DOMDocument without the enclosing , body, and

tags. However, saveXML() tends to add these wrappers when used with getElementsByTagName('p').

Alternative Solution for PHP 5.4 and Above

With PHP 5.4 and Libxml 2.6 or higher, you can utilize the loadHTML() function with additional options to prevent implied HTML elements and doctype:

1

$html->loadHTML($content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);

Copy after login

Using these options, saveHTML() will not produce the unwanted wrappers.

Explanation of Options:

  • LIBXML_HTML_NOIMPLIED: Disables adding automatic implied html and body elements.
  • LIBXML_HTML_NODEFDTD: Prevents creating a default doctype if none is present in the input content.

Additional Notes:

  • Documentation for Libxml parameters can be found here.
  • Note that while loadHTML documentation mentions Libxml 2.6 as a requirement, LIBXML_HTML_NODEFDTD became available in Libxml 2.7.8, and LIBXML_HTML_NOIMPLIED was available from Libxml 2.7.7 onwards.

The above is the detailed content of How Can I Save a DOMDocument's HTML Content in PHP Without the Default HTML, Body, and P Tags?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template