A tokeniser based HTML document parser and minifier, written in PHP.
An HTML parser, primarily designed for minifying HTML documents, it also enables the document structure to be queried allowing attribute and textnode values to be extracted.
The parser is designed around a tokeniser to make the document processing more reliable than regex based minifiers, which are a bit blunt and can be problematic if they match patterns in the wrong places.
The software is also capable of processing and minifying SVG documents.
To minify an HTML document:
use hexydec\html\htmldoc;
$doc = new htmldoc();
// load from a variable
if ($doc->load($html) {
// minify the document
$doc->minify();
// compile back to HTML
echo $doc->save();
}
You can test out the minifier online at https://hexydec.com/apps/minify-html/, or run the supplied index.php
file after installation.
To extract data from an HTML document:
use hexydec\html\htmldoc;
$doc = new htmldoc();
// load from a URL this time
if ($doc->open($url) {
// extract text
$text = $doc->find('.article__body')->text();
// extract attribute
$attr = $doc->find('.article__author-image')->attr('src');
// extract HTML
$html = $doc->find('.article__body')->html();
}
The easiest way to get up and running is to use composer:
$ composer install hexydec/htmldoc
HTMLdoc requires \hexydec\token\tokenise to run, which you can install manually if not using composer. Optionally you can also install CSSdoc and JSlite to perform inline CSS and Javascript minification respectively.
All these dependencies will be installed through composer.
You can run the test suite like this:
$ vendor/bin/phpunit
> vendor\bin\phpunit
- How it works
- How to use and examples
- API Reference
- Mitigating Side Effects of Minification
- About Document Recycling
- Object Performance
HTMLdoc supports PHP version 8.0+.
If you find an issue with HTMLdoc, please create an issue in the tracker.
If you wish to fix an issue yourself, please fork the code, fix the issue, then create a pull request, and I will evaluate your submission.
The MIT License (MIT). Please see License File for more information.