javascript minifiershtml minifiers
Rodrigo54 PHPClasses
css minifiers
javascript css html minifier evaluation
Written: Jun-2023

HTML Code Minifier Discussion

Continuing on from evaluating the set of modules available for JavaScript minification, the need for HTML minification seemed another easy secondary solution that would help in reducing the noise being sent over the wire to the client browser. The evaluation was also more simple than the JavaScript - looking for (1) does it result in valid HTML, (2) does it remove block comments and (3) does it remove important block comments.

ClassValid
result
Block-
comments
Minifies
<style>
contents
Minifies
<script>
contents
Removes
CRs
Test
speed**
Result
size
JSqueeze n/a n/a n/a n/a n/a n/a n/a
Rodrigo54 check check check1 check1 check 0.0008 62.11%
PHPClasses/9865 check check close close check 0.00062 65.78%2
JShrink n/a n/a n/a n/a n/a n/a n/a
PHPWee n/a n/a n/a n/a n/a n/a n/a
MatthiasMullie n/a n/a n/a n/a n/a n/a n/a
CSSTidy n/a n/a n/a n/a n/a n/a n/a

2 This is computed based on the output without the <!DOCTYPE tag
** This will change and was simply a snapshot of the basic [small code] test run at one time.

Scroll down, or click on the links in the table above to jump to the live-demo of each class involved.

Rodrigo54

https://gist.github.com/Rodrigo54

Code implementation is very simple:

rodrigo54-html.php
require_once 'rodrigo54-minifier.php'; // or whatever filename you chose
$jz = new Rodrigo54();

$minifiedCode = $jz->minify_html($html);
// or $minifiedCode = $jz->minify_css($css);
// or $minifiedCode = $jz->minify_html($html);
Pros

  • Very easy deployment (note I decided to clipboard the raw-code into a Rodrigo54 class so that I could use it as an external class in my packages).
  • Very simple and easy to understand code involving a set of regex's.
  • Handles HTML and CSS as well as JavaScript, calling itself recursively when it detects <style>...</style> and <script>...</script> blocks.
  • Removes comments and line breaks between tags
  • Does what is says on the tin.

Cons

  • No version number in the code or download, so you don't know if you're using older code.

PHPClasses

(package/9865)https://www.phpclasses.org

Code implementation is very simple:

phpclasses-html.php
require_once 'phpclasses9865/HtmlMinifier.phpclass'; //
$minifier        =  new  HtmlMinifier () ;
$minifiedCode    =  $minifier->Minify( $html ) ;
// or $minifiedCode    =  $minifier->MinifyFrom( $filename ) ;
I'm not a fan of this one mainly due to the complexity of the code verses what it delivers, and the confusion of documentation that PHPClasses.org provides.

Pros

  • It will remove line CRs

Cons

  • Download requires a PHPClasses.org login before you can evaluate the solution.
  • PHPClasses documentation and download area is a disaster and difficult to navigate.
  • Strange use of .phpclass files (something I'm not familiar with).
  • Documentation.
  • No version number in the code or download, so you don't know if you're using older code.
  • Not clear if any fixes have been applied to this code since first posting.


link Click to view a demo

square