PHP Classes

PHP HTML Minify Class: Compress HTML removing unnecessary data

Recommend this page to a friend!
  Info   View files Documentation   View files View files (36)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2022-10-21 (8 days ago) RSS 2.0 feedStarStarStarStar 65%Total: 470 This week: 7All time: 5,994 This week: 106Up
Version License PHP version Categories
htmlmin 1.0.37MIT/X Consortium ...5.3HTML, PHP 5, Compression
Collaborate with this project Author

HtmlMin - github.com

Description

This class can compress HTML removing unnecessary data.

It can take a given HTML string and perform certain operations to reduce the size of the same document.

Currently it can remove comments, default attributes, anchor names, script charset attributes, the document charset attributes, type from script tags, script mime types, type from style sheet link, empty attributes, http prefix from attributes, value from empty input, whitespace around tags.

It can also sort CSS class names HTML attributes for better gzip compression, and sum up white space.

Innovation Award
PHP Programming Innovation award nominee
November 2016
Number 4
Compressing the HTML of the pages is one way to increase the speed of serving Web pages.

Most packages that perform this task, simply try to remove whitespaces and comments, but this is only one way to reduce the the size of the final HTML.

This package goes father by performing less obvious compression approaches like default removing attributes, changing anchor names to id, removing script and document charset attributes, removing types from script tags and style sheet link, empty attributes, removing http prefix from attributes, and removing values from empty input.

Manuel Lemos
Picture of Lars Moelleken
  Performance   Level  
Name: Lars Moelleken <contact>
Classes: 25 packages by
Country: Germany Germany
Innovation award
Innovation award
Nominee: 11x

Winner: 1x

Details

Build Status Coverage Status Codacy Badge Latest Stable Version Total Downloads License Donate to this project using Paypal Donate to this project using Patreon

:clamp: HtmlMin: HTML Compressor and Minifier for PHP

Description

HtmlMin is a fast and very easy to use PHP library that minifies given HTML5 source by removing extra whitespaces, comments and other unneeded characters without breaking the content structure. As a result pages become smaller in size and load faster. It will also prepare the HTML for better gzip results, by re-ranging (sort alphabetical) attributes and css-class-names.

Install via "composer require"

composer require voku/html-min

Quick Start

use voku\helper\HtmlMin;

$html = "
<html>
  \r\n\t
  <body>
    <ul style=''>
      <li style='display: inline;' class='foo'>
        \xc3\xa0
      </li>
      <li class='foo' style='display: inline;'>
        \xc3\xa1
      </li>
    </ul>
  </body>
  \r\n\t
</html>
";
$htmlMin = new HtmlMin();

echo $htmlMin->minify($html); 
// '<html><body><ul><li class=foo style="display: inline;"> à <li class=foo style="display: inline;"> á </ul>'

Options

use voku\helper\HtmlMin;

$htmlMin = new HtmlMin();

/* 
 * Protected HTML (inline css / inline js / conditional comments) are still protected,
 *    no matter what settings you use.
 */

$htmlMin->doOptimizeViaHtmlDomParser();               // optimize html via "HtmlDomParser()"
$htmlMin->doRemoveComments();                         // remove default HTML comments (depends on "doOptimizeViaHtmlDomParser(true)")
$htmlMin->doSumUpWhitespace();                        // sum-up extra whitespace from the Dom (depends on "doOptimizeViaHtmlDomParser(true)")
$htmlMin->doRemoveWhitespaceAroundTags();             // remove whitespace around tags (depends on "doOptimizeViaHtmlDomParser(true)")
$htmlMin->doOptimizeAttributes();                     // optimize html attributes (depends on "doOptimizeViaHtmlDomParser(true)")
$htmlMin->doRemoveHttpPrefixFromAttributes();         // remove optional "http:"-prefix from attributes (depends on "doOptimizeAttributes(true)")
$htmlMin->doRemoveHttpsPrefixFromAttributes();        // remove optional "https:"-prefix from attributes (depends on "doOptimizeAttributes(true)")
$htmlMin->doKeepHttpAndHttpsPrefixOnExternalAttributes(); // keep "http:"- and "https:"-prefix for all external links 
$htmlMin->doMakeSameDomainsLinksRelative(['example.com']); // make some links relative, by removing the domain from attributes
$htmlMin->doRemoveDefaultAttributes();                // remove defaults (depends on "doOptimizeAttributes(true)" | disabled by default)
$htmlMin->doRemoveDeprecatedAnchorName();             // remove deprecated anchor-jump (depends on "doOptimizeAttributes(true)")
$htmlMin->doRemoveDeprecatedScriptCharsetAttribute(); // remove deprecated charset-attribute - the browser will use the charset from the HTTP-Header, anyway (depends on "doOptimizeAttributes(true)")
$htmlMin->doRemoveDeprecatedTypeFromScriptTag();      // remove deprecated script-mime-types (depends on "doOptimizeAttributes(true)")
$htmlMin->doRemoveDeprecatedTypeFromStylesheetLink(); // remove "type=text/css" for css links (depends on "doOptimizeAttributes(true)")
$htmlMin->doRemoveDeprecatedTypeFromStyleAndLinkTag(); // remove "type=text/css" from all links and styles
$htmlMin->doRemoveDefaultMediaTypeFromStyleAndLinkTag(); // remove "media="all" from all links and styles
$htmlMin->doRemoveDefaultTypeFromButton();            // remove type="submit" from button tags 
$htmlMin->doRemoveEmptyAttributes();                  // remove some empty attributes (depends on "doOptimizeAttributes(true)")
$htmlMin->doRemoveValueFromEmptyInput();              // remove 'value=""' from empty <input> (depends on "doOptimizeAttributes(true)")
$htmlMin->doSortCssClassNames();                      // sort css-class-names, for better gzip results (depends on "doOptimizeAttributes(true)")
$htmlMin->doSortHtmlAttributes();                     // sort html-attributes, for better gzip results (depends on "doOptimizeAttributes(true)")
$htmlMin->doRemoveSpacesBetweenTags();                // remove more (aggressive) spaces in the dom (disabled by default)
$htmlMin->doRemoveOmittedQuotes();                    // remove quotes e.g. class="lall" => class=lall
$htmlMin->doRemoveOmittedHtmlTags();                  // remove ommitted html tags e.g. <p>lall</p> => <p>lall 

PS: you can use the "nocompress"-tag to keep the html e.g.: "<nocompress>\n foobar \n</nocompress>"

Unit Test

1) Composer is a prerequisite for running the tests.

composer require voku/html-min

2) The tests can be executed by running this command from the root directory:

./vendor/bin/phpunit

Support

For support and donations please visit Github | Issues | PayPal | Patreon.

For status updates and release announcements please visit Releases | Twitter | Patreon.

For professional support please contact me.

Thanks

  • Thanks to GitHub (Microsoft) for hosting the code and a good infrastructure including Issues-Managment, etc.
  • Thanks to IntelliJ as they make the best IDEs for PHP and they gave me an open source license for PhpStorm!
  • Thanks to Travis CI for being the most awesome, easiest continous integration tool out there!
  • Thanks to StyleCI for the simple but powerfull code style check.
  • Thanks to PHPStan && Psalm for relly great Static analysis tools and for discover bugs in the code!
  Files folder image Files  
File Role Description
Files folder image.github (4 files, 1 directory)
Files folder imagesrc (1 directory)
Files folder imagetests (2 files, 1 directory)
Accessible without login Plain text file .editorconfig Data Auxiliary data
Accessible without login Plain text file .scrutinizer.yml Data Auxiliary data
Accessible without login Plain text file .styleci.yml Data Auxiliary data
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file CHANGELOG.md Data Auxiliary data
Accessible without login Plain text file circle.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpcs.php_cs Example Example script
Accessible without login Plain text file phpstan.neon Data Auxiliary data
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation
Accessible without login Plain text file renovate.json Data Auxiliary data

  Files folder image Files  /  .github  
File Role Description
Files folder imageworkflows (1 file)
  Accessible without login Plain text file CONTRIBUTING.md Data Auxiliary data
  Accessible without login Plain text file FUNDING.yml Data Auxiliary data
  Accessible without login Plain text file ISSUE_TEMPLATE.md Data Auxiliary data
  Accessible without login Plain text file PULL_REQUEST_TEMPLATE.md Data Auxiliary data

  Files folder image Files  /  .github  /  workflows  
File Role Description
  Accessible without login Plain text file ci.yml Data Auxiliary data

  Files folder image Files  /  src  
File Role Description
Files folder imagevoku (1 directory)

  Files folder image Files  /  src  /  voku  
File Role Description
Files folder imagehelper (4 files)

  Files folder image Files  /  src  /  voku  /  helper  
File Role Description
  Plain text file HtmlMin.php Class Class source
  Plain text file HtmlMinDomObserverInterface.php Class Class source
  Plain text file HtmlMinDomObserverOptimizeAttributes.php Class Class source
  Plain text file HtmlMinInterface.php Class Class source

  Files folder image Files  /  tests  
File Role Description
Files folder imagefixtures (12 files)
  Accessible without login Plain text file bootstrap.php Aux. Auxiliary script
  Accessible without login Plain text file HtmlMinTest.php Test Unit test script

  Files folder image Files  /  tests  /  fixtures  
File Role Description
  Accessible without login Plain text file base1.html Data Auxiliary data
  Accessible without login Plain text file base1_result.html Data Auxiliary data
  Accessible without login Plain text file base2.html Data Auxiliary data
  Accessible without login Plain text file base2_result.html Data Auxiliary data
  Accessible without login Plain text file base3.html Data Auxiliary data
  Accessible without login Plain text file base3_result.html Data Auxiliary data
  Accessible without login HTML file base4.html Doc. Documentation
  Accessible without login HTML file base4_result.html Doc. Documentation
  Accessible without login Plain text file code.html Data Auxiliary data
  Accessible without login Plain text file code_result.html Data Auxiliary data
  Accessible without login Plain text file hlt.html Data Auxiliary data
  Accessible without login Plain text file hlt_result.html Data Auxiliary data

Downloadhtmlmin-2022-10-21.zip 77KB
Downloadhtmlmin-2022-10-21.tar.gz 61KB
Install with ComposerInstall with Composer
Needed packages  
Class DownloadWhy it is needed Dependency
Simple HTML DOM Download .zip .tar.gz Dom-Parsing Required
 Version Control Unique User Downloads Download Rankings  
 100%
Total:470
This week:7
All time:5,994
This week:106Up
 User Ratings  
 
 All time
Utility:80%StarStarStarStarStar
Consistency:85%StarStarStarStarStar
Documentation:85%StarStarStarStarStar
Examples:-
Tests:80%StarStarStarStarStar
Videos:-
Overall:65%StarStarStarStar
Rank:822
  
For more information send a message to info at phpclasses dot org.