PHPackages                             mallardduck/html-formatter - 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. mallardduck/html-formatter

ActiveLibrary

mallardduck/html-formatter
==========================

HTML beautifier and minifier library for development and testing.

1.0.1(4y ago)05.1k3MITPHPPHP ^7.4 || ^8.0

Since Sep 28Pushed 4y agoCompare

[ Source](https://github.com/mallardduck/html-formatter)[ Packagist](https://packagist.org/packages/mallardduck/html-formatter)[ Docs](https://github.com/mallardduck/html-formatter)[ GitHub Sponsors](https://github.com/navindex)[ RSS](/packages/mallardduck-html-formatter/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (2)Dependencies (5)Versions (9)Used By (3)

HTML Formatter [![Latest Version](https://camo.githubusercontent.com/41c9afb354eca7d6507aaaf2dc479e0b2254b6d6fa5f4c25174375a424c7fa3d/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f6d616c6c6172646475636b2f68746d6c2d666f726d61747465723f736f72743d73656d766572266c6162656c3d76657273696f6e)](https://raw.githubusercontent.com/mallardduck/html-formatter/master/CHANGELOG.md)
==============================================================================================================================================================================================================================================================================================================================================================================================================

[](#html-formatter-)

[![Unit tests](https://github.com/mallardduck/html-formatter/actions/workflows/test.yml/badge.svg)](https://github.com/mallardduck/html-formatter/actions/workflows/test.yml)[![Code analysis](https://github.com/mallardduck/html-formatter/actions/workflows/analysis.yml/badge.svg)](https://github.com/mallardduck/html-formatter/actions/workflows/analysis.yml)[![Coverage Status](https://camo.githubusercontent.com/3d439c59c3148e4836343eab03dcb625b01e6fe97b0fb7a534c76ba0b3b2a09b/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f6d616c6c6172646475636b2f68746d6c2d666f726d61747465722f62616467652e7376673f6272616e63683d6d61696e)](https://coveralls.io/github/mallardduck/html-formatter?branch=master)[![License: MIT](https://camo.githubusercontent.com/d6bc2b26794002c24d023acaab01b6dbb953c57ab9cb80ba5b8aa2f2bd5de99a/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d626c7565)](https://opensource.org/licenses/MIT)

1. What Is It
-------------

[](#1-what-is-it)

HTML Formatter will beautify or minify your HTML string for development and testing. Dedicated for those who suffer from reading a template engine produced markup.

2. What Is It Not
-----------------

[](#2-what-is-it-not)

HTML Formatter will not sanitize or otherwise manipulate your output beyond indentation and whitespace replacement. HTML Formatter will only add indentation, without otherwise affecting the markup.

If you are looking to remove malicious code or make sure that your document is standards compliant, consider the following alternatives:

- [HTML Purifier](https://github.com/Exercise/HTMLPurifierBundle)
- [DOMDocument::$formatOutput](http://www.php.net/manual/en/class.domdocument.php)
- [Tidy](http://www.php.net/manual/en/book.tidy.php)
- [Chrome DevTools](https://developers.google.com/chrome-developer-tools/)

If you need to format your code in the development environment, beware that earlier mentioned libraries will attempt to fix your markup.

3. Installation
---------------

[](#3-installation)

This package can be installed through [Composer](https://getcomposer.org/).

```
composer require mallardduck/html-formatter
```

4. Usage
--------

[](#4-usage)

```
use MallardDuck\HtmlFormatter\Formatter;

$input = 'This is your HTML code.';
$formatter = new Formatter();
$output = $formatter->beautify($input);
```

5. Configuration
----------------

[](#5-configuration)

`Formatter` is using **[Simple config](https://github.com/navindex/simple-config)** to adjust its configuration settings.

NameTypeDescription`tab`stringCharacter(s) used for indentation. Defaults to 4 spaces.`self-closing.tag`arrayHere you can add more self-closing tags. [see below](#5-1)`inline.tag`arrayHere you can add more inline tags, or change any block tags and make them inline. [see below](#5-2)`formatted.tag`arrayHere you can add more tags to be exluded of formatting. [see below](#5-3)`attributes.trim`booleanRemove leading and trailing whitespace in the attribute values.`attribute.cleanup`booleanReplace all whitespaces with a single space in the attribute values.`cdata.cleanup`booleanReplace all whitespaces with a single space in CDATA.`cdata.trim`booleanRemove leading and trailing whitespace in CDATA.

### 5.1. Inline and block elements

[](#51-inline-and-block-elements)

HTML elements are either "inline" elements or "block-level" elements.

An inline element occupies only the space bounded by the tags that define the inline element. The following example demonstrates the inline element's influence:

```
This is an inline element within a block element.
```

A block-level element occupies the entire space of its parent element (container), thereby creating a "block." Browsers typically display the block-level element with a new line both before and after the element. The following example demonstrates the block-level element's influence:

```

    This is a block element within a block element.

```

HTML Formatter identifies the following elements as "inline":

- `a`, `abbr`, `acronym`, `b`, `bdo`, `big`, `br`, `button`, `cite`, `code`, `dfn`, `em`,
- `i`, `img`, `kbd`, `label`, `samp`, `small`, `span`, `strong`, `sub`, `sup`, `tt`, `var`

This is a subset of the inline elements defined in the [MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Inline_elements). All other elements are treated as block.

You can set additional inline elements by adding them to the `inline.tag` option.

```
use MallardDuck\HtmlFormatter\Formatter;

$formatter = new Formatter();
$config = $formatter->getConfig();
$config->append('inline.tag', 'foo')->subtract('inline.tag', ['bar', 'baz']);
$formatter->setConfig($config);
```

### 5.2. Self-closing (empty) elements

[](#52-self-closing-empty-elements)

An empty element is an element from HTML, SVG, or MathML that cannot have any child nodes (i.e., nested elements or text nodes).

HTML Formatter identifies the following elements as "inline":

- `area`, `base`, `br`, `col`, `command`, `embed`, `hr`, `img`, `input`,
- `keygen`, `link`, `menuitem`, `meta`, `param`, `source`, `track`, `wbr`,
- `animate`, `stop`, `path`, `circle`, `line`, `polyline`, `rect`, `use`

This is a subset of the empty elements defined in the [MDN](https://developer.mozilla.org/en-US/docs/Glossary/empty_element). All other elements require closing tag.

You can set additional self-closing elements by adding them to the `self-closing.tag` option.

```
use MallardDuck\HtmlFormatter\Formatter;

$formatter = new Formatter();
$formatter->setConfig($formatter->getConfig()->append('self-closing.tag', ['foo', 'bar']));
```

### 5.3. Preformatted elements

[](#53-preformatted-elements)

Specific element will be not touched by the formatter. The built in preformatted elements are

- `script`, `pre`, `textarea`.

You can exclude additional elements by adding them to the `formatted.tag` option.

There settings for all the formatted tags are the following:

SettingTypeDescription`formatted.tag.cleanup-empty`booleanRemoves the inner content if it has only whitespace.`formatted.tag.opening-break`booleanInserts a line break after the opening tag.`formatted.tag.closing-break`booleanInserts a line break before the closing tag.`formatted.tag.trim`booleanRemoves the leading and trailing whitespace of the content.You can also change the settings for a specific tag. For example, disabling the `cleanup-empty` setting for the `script` tag looks like this:

```
use MallardDuck\HtmlFormatter\Formatter;

$formatter = new Formatter();
$config = $formatter->getConfig();
$config->set('formatted.tag.script.cleanup-empty', false);
$formatter->setConfig($config);
```

### 5.4. Attributes

[](#54-attributes)

Additional settings for formatted tags are the following:

SettingTypeDescription`cleanup-empty`booleanRemoves the inner content if it has only whitespace.`opening-break`booleanInserts a line break after the opening tag.`closing-break`booleanInserts a line break before the closing tag.`trim`booleanRemoves the leading and trailing whitespace of the content.6. Methods
----------

[](#6-methods)

NameAttributesDescriptionExample`constructor`Config|array|nullCreates a `Formatter` class instance`$f = new Formatter($config);``getConfig`-Configuration getter`$config = $f->getConfig();``getConfigArray`-Configuration getter`$config = $f->getConfigArray();``setConfig`Config|array|nullConfiguration setter`$f->setConfig($config);``beautify`stringBeautifies the input string`$output = $f->beautify($html);``minify`stringMinifies the input string`$output = $f->minify($html);`7. Credits
----------

[](#7-credits)

Thanks to **[Gajus Kuizinas](https://github.com/gajus)** for originally creating [gajus/dindent](https://github.com/gajus/dindent) and all the other developers who are tirelessly working on it. HTML Formatter was inspired by Dindent.

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity22

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 85.4% 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 ~50 days

Total

4

Last Release

1541d ago

Major Versions

v0.1.1 → 1.0.02022-02-27

PHP version history (2 changes)v0.1.0PHP ^7.3|^8.0

1.0.0PHP ^7.4 || ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/801be8ba9bfb0cb2629f51f9753a0392683c2680fe3e941a188495840d920333?d=identicon)[MallardDuck](/maintainers/MallardDuck)

---

Top Contributors

[![balinomad](https://avatars.githubusercontent.com/u/26588579?v=4)](https://github.com/balinomad "balinomad (88 commits)")[![mallardduck](https://avatars.githubusercontent.com/u/619938?v=4)](https://github.com/mallardduck "mallardduck (15 commits)")

---

Tags

htmlminifyformatindentbeautify

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/mallardduck-html-formatter/health.svg)

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

###  Alternatives

[renatomarinho/laravel-page-speed

Laravel Page Speed

2.5k1.7M10](/packages/renatomarinho-laravel-page-speed)[gajus/dindent

HTML indentation library for development and testing.

1781.9M50](/packages/gajus-dindent)[netcarver/textile

Textile markup language parser

2311.5M16](/packages/netcarver-textile)[fitztrev/laravel-html-minify

Minifies the HTML output of Laravel 4 applications

414211.4k](/packages/fitztrev-laravel-html-minify)[vinkius-labs/laravel-page-speed

Laravel Page Speed

2.5k5.4k1](/packages/vinkius-labs-laravel-page-speed)[nochso/html-compress-twig

Twig extension for compressing HTML and inline CSS/Javascript

84468.6k8](/packages/nochso-html-compress-twig)

PHPackages © 2026

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