Home > Backend Development > PHP Tutorial > php DOMDocument recursively formats indented HTML documents

php DOMDocument recursively formats indented HTML documents

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-29 09:02:03
Original
1173 people have browsed it

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();
Copy after login

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.

Related labels:
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 Issues
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template