php DOMDocument recursively formats indented HTML documents
php DOMDocument recursively formats indented HTML documents
<span>function</span> format(\DOMNode <span>$node</span>, <span>$treeIndex</span> = 0<span>) { </span><span>//</span><span>不格式化的标签</span><span>if</span> (<span>in_array</span>(<span>$node</span>->nodeName, <span>array</span>("title", "p", "span"<span>))) </span><span>return</span><span>; </span><span>if</span> (<span>$node</span>-><span>hasChildNodes()) { </span><span>$treeIndex</span>++<span>; </span><span>$tabStart</span> = "\r\n" . <span>str_repeat</span>(" ", <span>$treeIndex</span><span>); </span><span>$tabEnd</span> = "\r\n" . <span>str_repeat</span>(" ", <span>$treeIndex</span> - 1<span>); </span><span>$i</span> = 0<span>; </span><span>while</span> (<span>$childNode</span> = <span>$node</span>->childNodes->item(<span>$i</span>++<span>)) { </span><span>//</span><span>去除空的text node</span><span>if</span> (<span>$childNode</span>->nodeType == XML_TEXT_NODE and <span>preg_match</span>('#^\s*$#', <span>$childNode</span>-><span>nodeValue)) { </span><span>$node</span>->removeChild(<span>$childNode</span><span>); </span><span>$i</span>--<span>; </span><span>continue</span><span>; } </span><span>$node</span>->insertBefore(<span>$node</span>->ownerDocument->createTextNode(<span>$tabStart</span>), <span>$childNode</span><span>); </span><span>$i</span>++<span>; format(</span><span>$childNode</span>, <span>$treeIndex</span><span>); }; </span><span>$node</span>->appendChild(<span>$node</span>->ownerDocument->createTextNode(<span>$tabEnd</span><span>)); } } </span><span>$html</span> = '<!DOCTYPE html><html><head><meta charset="utf-8"><title></title></head><body></body></html>'<span>; </span><span>$doc</span> = <span>new</span><span> \DOMDocument(); </span><span>//</span><span>$doc->formatOutput = true; //不知道是不是我的理解问题,这个选项格式化出来的并不完美</span><span>$doc</span>->loadHTML(<span>$html</span><span>); format(</span><span>$doc</span>-><span>documentElement); </span><span>echo</span><span>$doc</span>->saveHTML();
The above introduces the php DOMDocument recursive formatting and indentation of HTML documents, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

Detailed explanation and installation guide for PiNetwork nodes This article will introduce the PiNetwork ecosystem in detail - Pi nodes, a key role in the PiNetwork ecosystem, and provide complete steps for installation and configuration. After the launch of the PiNetwork blockchain test network, Pi nodes have become an important part of many pioneers actively participating in the testing, preparing for the upcoming main network release. If you don’t know PiNetwork yet, please refer to what is Picoin? What is the price for listing? Pi usage, mining and security analysis. What is PiNetwork? The PiNetwork project started in 2019 and owns its exclusive cryptocurrency Pi Coin. The project aims to create a one that everyone can participate

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.
