• Stars
    star
    143
  • Rank 257,007 (Top 6 %)
  • Language
    PHP
  • License
    MIT License
  • Created about 8 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

๐Ÿ—œ๏ธ HtmlMin: HTML Compressor and Minifier via PHP

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

๐Ÿ—œ๏ธ 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.: "\n foobar \n"

Unit Test

  1. Composer is a prerequisite for running the tests.
composer require voku/html-min
  1. 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!

More Repositories

1

anti-xss

ใŠ™๏ธ AntiXSS | Protection against Cross-site scripting (XSS) via PHP
PHP
623
star
2

portable-utf8

๐Ÿ‰‘ Portable UTF-8 library - performance optimized (unicode) string functions for PHP.
PHP
490
star
3

Arrayy

๐Ÿ—ƒ Array manipulation library for PHP, called Arrayy!
PHP
462
star
4

portable-ascii

๐Ÿ”ก Portable ASCII library - performance optimized (ascii) string functions for PHP.
PHP
413
star
5

stop-words

PHP | A collection of stop words for e.g. search-functions.
PHP
76
star
6

PHPDoctor

๐Ÿฅ PHPDoctor: Check files, full directories or strings for missing or bad PHPDoc types.
PHP
53
star
7

email-check

โœ‰๏ธ E-Mail Address Validator (syntax, dns, trash, typo)
PHP
45
star
8

Simple-PHP-Code-Parser

โค Simple PHP Code Parser | A simple data structure from your PHP code.
PHP
40
star
9

simple-cache

โšก Simple Cache Abstraction Layer for PHP
PHP
29
star
10

phpstan-rules

Provides additional rules for phpstan
PHP
23
star
11

CONFIG--nginx---php-fpm---mysql

my config for WordPress Multi with nginx + php-fpm
Shell
20
star
12

phonetic-algorithms

Phonetic-Algorithms for fuzzy searching | PHP
PHP
16
star
13

anti-xss-twig

AntiXSS for Twig
PHP
11
star
14

weather

PHP
6
star
15

Php-Readme-Helper

๐Ÿ“– PHP README Helper
PHP
5
star
16

awesome-web

A curated list of awesome lists / links / feeds and more ...
5
star
17

php-doc-fixer

This is an experiment! Let's check / fix the PHP documentation automatically.
PHP
4
star
18

simple-active-record

๐Ÿ’ Active Record Pattern via PHP
PHP
4
star
19

value_objects

PHP
3
star
20

stoppropaganda

๐Ÿ‡บ๐Ÿ‡ฆ
PHP
3
star
21

HtmlFormValidator

๐Ÿ”ฆ HtmlFormValidator | HTML Form Validation via DOM Parsing
PHP
3
star
22

Android-Tweak

2
star
23

Web-Analyse-Tools-for-Chrome

Chome: This extension shows links to some good Website-Analysis-Tools for the current tab.
JavaScript
2
star
24

android_packages_apps_GenKiller

SpicagenMod task manager
Java
2
star
25

external-alsa-utils

C
1
star
26

diridea

๐Ÿ“ Diridea: process your different directory types
PHP
1
star
27

DO-EPIC-SHIT-Demo

Demo: Open Source Workflow fรผr Webdeveloper (OpenRheinRuhr 2014)
CSS
1
star
28

external-alsa-lib

C
1
star
29

snappy

https://code.google.com/p/snappy/
C
1
star
30

DO-EPIC-SHIT

Presentation: Open Source Workflow fรผr Webdeveloper (OpenRheinRuhr 2014)
CSS
1
star
31

desing_pattern_frontend

CSS
1
star
32

weather-demo

Demo for https://github.com/voku/weather
PHP
1
star