PHPackages                             akankov/html-min - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. akankov/html-min

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

akankov/html-min
================

HTML compressor and minifier for PHP 8.3+

v2.8.0(1w ago)267.7k↓14.5%2MITHTMLPHP 8.3.\* || 8.4.\* || 8.5.\*CI passing

Since Apr 17Pushed yesterdayCompare

[ Source](https://github.com/akankov/html-min)[ Packagist](https://packagist.org/packages/akankov/html-min)[ Docs](https://github.com/akankov/html-min)[ Fund](https://ko-fi.com/alekseikankov)[ RSS](/packages/akankov-html-min/feed)WikiDiscussions main Synced 1w ago

READMEChangelog (10)Dependencies (42)Versions (39)Used By (2)

[![CI](https://github.com/akankov/html-min/actions/workflows/ci.yml/badge.svg)](https://github.com/akankov/html-min/actions/workflows/ci.yml)[![codecov](https://camo.githubusercontent.com/25050af9956fd05769f53b8c9c838b3bee6080182ce67f737649d9bb3cd6a7ac/68747470733a2f2f636f6465636f762e696f2f67682f616b616e6b6f762f68746d6c2d6d696e2f6272616e63682f6d61696e2f67726170682f62616467652e737667)](https://codecov.io/gh/akankov/html-min)[![Mutation testing badge](https://camo.githubusercontent.com/1ae9debf9929e4f30fbfbdfad08b3bb143cd657106e0e96552cf49dc2e33692c/68747470733a2f2f696d672e736869656c64732e696f2f656e64706f696e743f7374796c653d666c61742675726c3d687474707325334125324625324662616467652d6170692e737472796b65722d6d757461746f722e696f2532466769746875622e636f6d253246616b616e6b6f7625324668746d6c2d6d696e2532466d61696e)](https://dashboard.stryker-mutator.io/reports/github.com/akankov/html-min/main)[![Latest Stable Version](https://camo.githubusercontent.com/9f7f612defa6317dc60bc4150d75463df901d60dd9cd9d79802cc8a5be55eb83/687474703a2f2f706f7365722e707567782e6f72672f616b616e6b6f762f68746d6c2d6d696e2f76)](https://packagist.org/packages/akankov/html-min)[![Monthly Downloads](https://camo.githubusercontent.com/8e60c00366a824cf7b3431de752e21c38715db7e01a01e40a9e7d2e720c77241/687474703a2f2f706f7365722e707567782e6f72672f616b616e6b6f762f68746d6c2d6d696e2f642f6d6f6e74686c79)](https://packagist.org/packages/akankov/html-min)[![Dependents](https://camo.githubusercontent.com/16b7bef826c3a9b33976fc4cc865e9cb83f08360d0750fe2bb6f31b9ac6c4811/687474703a2f2f706f7365722e707567782e6f72672f616b616e6b6f762f68746d6c2d6d696e2f646570656e64656e7473)](https://packagist.org/packages/akankov/html-min)[![License](https://camo.githubusercontent.com/ccf37ea7247f99c175ad292e843b994d8df49ec3455d4da685308cb00d6968ea/687474703a2f2f706f7365722e707567782e6f72672f616b616e6b6f762f68746d6c2d6d696e2f6c6963656e7365)](https://packagist.org/packages/akankov/html-min)

html-min
========

[](#html-min)

A fast HTML5 compressor and minifier for PHP. Strips redundant whitespace, comments, optional tags, and default attributes, then sorts what's left so your gzip layer has less work to do.

Built on native `\DOMDocument` — no third-party DOM dependencies.

Requirements
------------

[](#requirements)

- PHP **8.3**, 8.4, or 8.5
- ext-dom, ext-libxml, ext-mbstring

Installation
------------

[](#installation)

```
composer require akankov/html-min
```

Usage
-----

[](#usage)

```
use Akankov\HtmlMin\HtmlMin;

$html = doRemoveComments(true)             // drop HTML comments (conditional comments are preserved)
    ->doSumUpWhitespace(true)            // collapse runs of whitespace in text nodes
    ->doRemoveWhitespaceAroundTags(false)// aggressive: also trim whitespace adjacent to block tags
    ->doRemoveSpacesBetweenTags(false)   // aggressive: remove whitespace-only text nodes between elements

    // Attribute optimization
    ->doOptimizeAttributes(true)
    ->doSortHtmlAttributes(true)         // canonical attribute order → better gzip
    ->doSortCssClassNames(true)          // canonical class order → better gzip
    ->doRemoveOmittedQuotes(true)        // class="foo" → class=foo when safe
    ->doRemoveOmittedHtmlTags(true)      // x → x where the closing tag is optional
    ->doRemoveEmptyAttributes(true)
    ->doRemoveValueFromEmptyInput(true)
    ->doRemoveDefaultAttributes(false)   // opt-in: drop defaults like form method=get

    // URL attribute trimming
    ->doRemoveHttpPrefixFromAttributes(false)
    ->doRemoveHttpsPrefixFromAttributes(false)
    ->doKeepHttpAndHttpsPrefixOnExternalAttributes(false)
    ->doMakeSameDomainsLinksRelative([])       // e.g. ['example.com'] → strip host from same-site links

    // Deprecated attribute cleanup
    ->doRemoveDeprecatedAnchorName(true)
    ->doRemoveDeprecatedScriptCharsetAttribute(true)
    ->doRemoveDeprecatedTypeFromScriptTag(true)
    ->doRemoveDeprecatedTypeFromStylesheetLink(true)
    ->doRemoveDeprecatedTypeFromStyleAndLinkTag(true)
    ->doRemoveDefaultMediaTypeFromStyleAndLinkTag(true)
    ->doRemoveDefaultTypeFromButton(false)

    // Inline CSS / JS minification (opt-in, off by default)
    ->doMinifyInlineCss(false)           // minify the contents of inline  blocks
    ->doMinifyInlineJs(false);           // minify the contents of inline  blocks

echo $htmlMin->minify($html);
```

Each setter returns `$this`, so you can configure and call `minify()` in one chain.

Inline CSS and JS minification
------------------------------

[](#inline-css-and-js-minification)

By default the contents of `` and `` blocks round-trip untouched. Enable the two opt-in toggles to minify them:

```
$htmlMin = (new HtmlMin())
    ->doMinifyInlineCss(true)
    ->doMinifyInlineJs(true);

echo $htmlMin->minify($html);
```

The bundled minifiers are zero-dependency and conservative:

- **CSS** — strips `/* … */` comments and collapses whitespace; the contents of strings and `url(…)` are preserved.
- **JS** — removes comments and collapses horizontal whitespace while preserving newlines (so Automatic Semicolon Insertion is unaffected), strings, regex literals, and template literals. Identifiers are never renamed.

Scripts that are not JavaScript are left alone automatically: a ``whose `type` is, for example, `application/ld+json` or `text/x-template`, and any ``, passes through unminified.

### Using a different minifier

[](#using-a-different-minifier)

For aggressive minification (identifier renaming, dead-code removal), plug in a third-party tool with `setInlineCssMinifier()` / `setInlineJsMinifier()`. Each takes any `callable(string): string`; pass `null` to restore the bundled default.

```
use MatthiasMullie\Minify; // composer require matthiasmullie/minify

$htmlMin = (new HtmlMin())
    ->doMinifyInlineCss(true)
    ->doMinifyInlineJs(true)
    ->setInlineCssMinifier(static fn (string $css): string => (new Minify\CSS($css))->minify())
    ->setInlineJsMinifier(static fn (string $js): string => (new Minify\JS($js))->minify());

echo $htmlMin->minify($html);
```

If a bundled minifier throws, the original source is kept and a warning is sent to the PSR-3 logger (when one is set via `setLogger()`), so a minifier bug can never corrupt the page. User-supplied callables let their exceptions propagate.

Extending
---------

[](#extending)

To run your own pass over every element during minification, implement `Akankov\HtmlMin\Contract\DomObserver` and register it:

```
use Akankov\HtmlMin\Contract\DomObserver;
use Akankov\HtmlMin\Contract\HtmlMinInterface;
use Akankov\HtmlMin\HtmlMin;

final class StripDataTestIds implements DomObserver
{
    public function domElementBeforeMinification(\DOMElement $element, HtmlMinInterface $htmlMin): void
    {
    }

    public function domElementAfterMinification(\DOMElement $element, HtmlMinInterface $htmlMin): void
    {
        if ($element->hasAttribute('data-testid')) {
            $element->removeAttribute('data-testid');
        }
    }
}

$htmlMin = new HtmlMin();
$htmlMin->attachObserverToTheDomLoop(new StripDataTestIds());
echo $htmlMin->minify($html);
```

Benchmarks
----------

[](#benchmarks)

Measured against voku/html-min, wyrihaximus/html-compress, zaininnari/html-minifier, and abordage/html-min on a corpus of real-world HTML pages.

adaptermedian ms/opgeomean ms/opparse failuresavg gzipped ratioakankov/html-min1.91.90 / 1590.7%akankov/html-min (inline)2.12.50 / 1587.6%voku/html-min3.33.60 / 1590.7%wyrihaximus/html-compress6.07.50 / 15**87.0%**zaininnari/html-minifier9.58.30 / 1594.8%abordage/html-min †**0.2****0.2**0 / 1590.2%*The table above is regenerated by `make bench` from the latest run.*See [latest.md](latest.md) for the per-fixture detail (speed, peak memory, gzipped compression ratio, methodology, and non-claims). Reproduce with `make bench-install && make bench` (requires Docker).

Development
-----------

[](#development)

```
composer install
make md-check                 # markdown formatting (Docker)
vendor/bin/phpunit            # tests
vendor/bin/phpstan analyse    # static analysis (level max)
vendor/bin/php-cs-fixer fix   # code style
```

CI runs the full matrix (PHP 8.3 / 8.4 / 8.5) on every push and pull request.

License
-------

[](#license)

MIT — see [LICENSE](LICENSE).

Originally authored by Lars Moelleken; maintained in this fork by Alex Kankov.

###  Health Score

57

—

FairBetter than 98% of packages

Maintenance99

Actively maintained with recent releases

Popularity36

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 98.9% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~2 days

Total

17

Last Release

9d ago

Major Versions

v1.2.0 → v2.0.02026-04-29

PHP version history (2 changes)v1.0.0-alpha.1PHP ^8.3

v2.0.0PHP 8.3.\* || 8.4.\* || 8.5.\*

### Community

Maintainers

![](https://www.gravatar.com/avatar/ed013f7318c9a6acff086c2c588b6c4ed2089dac3df56d95884367ec79764001?d=identicon)[akankov](/maintainers/akankov)

---

Top Contributors

[![akankov](https://avatars.githubusercontent.com/u/1974569?v=4)](https://github.com/akankov "akankov (177 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (2 commits)")

---

Tags

compressionhtmlminifyminifiercompress

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan, Rector

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/akankov-html-min/health.svg)

```
[![Health](https://phpackages.com/badges/akankov-html-min/health.svg)](https://phpackages.com/packages/akankov-html-min)
```

###  Alternatives

[cakephp/cakephp

The CakePHP framework

8.8k19.1M1.7k](/packages/cakephp-cakephp)[tempest/framework

The PHP framework that gets out of your way.

2.2k31.1k11](/packages/tempest-framework)[typo3/cms

TYPO3 CMS is a free open source Content Management Framework initially created by Kasper Skaarhoj and licensed under GNU/GPL.

1.2k1.9M122](/packages/typo3-cms)[flow-php/flow

PHP ETL - Extract Transform Load - Data processing framework

84735.1k](/packages/flow-php-flow)[voku/html-min

HTML Compressor and Minifier

1884.9M57](/packages/voku-html-min)[shopware/core

Shopware platform is the core for all Shopware ecommerce products.

585.4M506](/packages/shopware-core)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
