Skip to content
forked from voku/HtmlMin

HTML Compressor and Minifier via PHP 5.3+

License

Notifications You must be signed in to change notification settings

lojaskd/HtmlMin

 
 

Repository files navigation

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)")
$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)")

Unit Test

  1. Composer is a prerequisite for running the tests.
composer install voku/html-min
  1. The tests can be executed by running this command from the root directory:
./vendor/bin/phpunit

About

HTML Compressor and Minifier via PHP 5.3+

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%