PHP Classes
elePHPant
Icontem

PHP HTML Minify Class: Compress HTML removing unnecessary data

Recommend this page to a friend!
  Info   View files Documentation   View files View files (25)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2017-10-06 (11 days ago) RSS 2.0 feedStarStarStarStar 71%Total: 322 This week: 4All time: 6,943 This week: 195Up
Version License PHP version Categories
htmlmin 1.0.9MIT/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
  Performance   Level  
Name: Lars Moelleken <contact>
Classes: 17 packages by
Country: Germany Germany
Age: 29
All time rank: 141793 in Germany Germany
Week rank: 8 Up1 in Germany Germany Up
Innovation award
Innovation award
Nominee: 8x

Details

Build Status Coverage Status Scrutinizer Code Quality Codacy Badge SensioLabsInsight Latest Stable Version Total Downloads Latest Unstable Version License

HTML Compressor and Minifier

Description

HtmlMin is a fast and very easy to use PHP5.3+ 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;

require_once 'composer/autoload.php';

$html = "<html>\r\n\t<body>\xc3\xa0</body>\r\n\t</html>";
$htmlMin = new HtmlMin();
echo $htmlMin->minify($html); // '<html> <body></body> </html>'

Options

$htmlMin = new HtmlMin();

/* 
 * Protected HTML (inlince 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->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->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)

Unit Test

1) Composer is a prerequisite for running the tests.

composer install voku/html-min

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

./vendor/bin/phpunit

  Files folder image Files  
File Role Description
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 circle.yml Data Auxiliary data
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file CONTRIBUTING.md Data Auxiliary data
Accessible without login Plain text file ISSUE_TEMPLATE.md Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file PULL_REQUEST_TEMPLATE.md Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  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 (1 file)

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

  Files folder image Files  /  tests  
File Role Description
Files folder imagefixtures (10 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 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-2017-10-06.zip 43KB
Downloadhtmlmin-2017-10-06.tar.gz
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:322
This week:4
All time:6,943
This week:195Up
 User Ratings  
 
 All time
Utility:87%StarStarStarStarStar
Consistency:93%StarStarStarStarStar
Documentation:93%StarStarStarStarStar
Examples:-
Tests:87%StarStarStarStarStar
Videos:-
Overall:71%StarStarStarStar
Rank:308