Download the PHP package commnetivity/purehtml without Composer
On this page you can find all versions of the php package commnetivity/purehtml. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download commnetivity/purehtml
More information about commnetivity/purehtml
Files in commnetivity/purehtml
Package purehtml
Short Description HTML Processing leveraging PHP's DOM functions
License GPL-3.0
Homepage https://commnetivity.com/labs/purehtml.html
Informations about the package purehtml
(actually, this class works, but the builds fail because there are no PHP Unit tests and instructions set up yet for Travis-CI)
PureHTML DOM Processor
This library leverages the speed and flexibility in using PHP's native DOM processor to let you parse, strip, splice and even beautify your HTML pages.
Installation
The easiest way to get started is using Composer to install commnetivity/purehtml:
Beautify DOM function, explained:
This is a PHP function that beautifies the output of an HTML document by adding indentation and line breaks to make it more readable. Here is a breakdown of how it works:
- The function takes two parameters:
$doc
, which is the HTML document as aDOMDocument
object, and$depth
, which is the current depth of the recursion (default value is 1). - It creates a new
DOMXPath
object to query the HTML document. - It loops through all the text nodes in the HTML document using XPath, and removes any leading or trailing whitespace (including newlines, carriage returns, and spaces) from their values. If the resulting value is an empty string, it removes the node entirely.
- It defines a recursive function called
$format
that takes three parameters:$dom
, which is theDOMDocument
object representing the HTML document,$currentNode
, which is the current node being formatted, and$depth
, which is the current depth of the recursion. - If
$currentNode
is false, it removes the first child node of$dom
(which is typically the<!DOCTYPE>
declaration) and sets$currentNode
to$dom
itself. - It determines whether the current node should be indented by checking if it is a text node and its parent only has one child node. If so, it sets
$indentCurrent
to false; otherwise, it sets it to true. - If
$indentCurrent
is true and$depth
is greater than 1, it creates a new text node with a newline and$depth
spaces and inserts it before$currentNode
. - It loops through all the child nodes of
$currentNode
and recursively calls$format
on each one, passing in$dom
, the child node, and$depth+1
. - If any of the child nodes was indented, it sets
$indentClosingTag
to true. - If
$indentClosingTag
is true, it creates a new text node with a newline and$depth
spaces (unless the current node is the<html>
tag), and appends it to$currentNode
. - The function calls
$format
with$doc
as the first argument and no second argument (which sets$currentNode
to false), effectively formatting the entire HTML document. - The function returns the formatted HTML document with a
<!DOCTYPE>
declaration at the beginning.