PHPackages                             shtrihstr/html-serializer - 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. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. shtrihstr/html-serializer

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

shtrihstr/html-serializer
=========================

Convert HTML to JSON

97162PHP

Since Aug 18Pushed 9y ago1 watchersCompare

[ Source](https://github.com/shtrihstr/html-serializer)[ Packagist](https://packagist.org/packages/shtrihstr/html-serializer)[ RSS](/packages/shtrihstr-html-serializer/feed)WikiDiscussions master Synced 4w ago

READMEChangelogDependenciesVersions (1)Used By (0)

HTML Serializer
===============

[](#html-serializer)

Convert HTML to Array or JSON

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

[](#installation)

```
$ composer require shtrihstr/html-serializer

```

Usage
-----

[](#usage)

### HTML to JSON

[](#html-to-json)

```
$html = new HtmlSerializer\Html('Hello World');
$json = $html->toJson();
```

#### Result

[](#result)

```
[
    {
        "node": "div",
        "attributes": {
            "class": "content"
        },
        "children": [
            {
                "node": "p",
                "children": [
                    {
                        "node": "text",
                        "text": "Hello World"
                    }
                ]
            },
            {
                "node": "img",
                "attributes": {
                    "src": "img.png",
                    "alt": "",
                    "class": "image"
                }
            }
        ]
    }
]
```

### HTML to Array

[](#html-to-array)

```
$array = $html->toArray();
```

#### Result

[](#result-1)

```
[
    [
        'node' => 'div',
        'attributes' => [
            'class' => 'content',
        ],
        'children' => [
            [
                'node' => 'p',
                'children' => [
                    [
                        'node' => 'text',
                        'text' => 'Hello World',
                    ],
                ]
            ],
            [
                'node' => 'img',
                'attributes' => [
                    'src' => 'img.png',
                    'alt' => '',
                    'class' => 'image',
                ]
            ],
        ],
    ],
]
```

### Inline CSS

[](#inline-css)

```
$html = new HtmlSerializer\Html('Hello World');
$html->parseCss(); // enabled by default
$json = $html->toJson();
```

#### Result

[](#result-2)

```
[
    {
        "node": "div",
        "attributes": {
            "style": {
                "color": "red",
                "background": "url(img.png?foo;bar)"
            }
        },
        "children": [
            {
                "node": "text",
                "text": "Hello World"
            }
        ]
    }
]
```

```
$html->parseCss(false);
```

#### Result

[](#result-3)

```
[
    {
        "node": "div",
        "attributes": {
            "style": "color: red; background: url(img.png?foo;bar)"
        },
        "children": [
            {
                "node": "text",
                "text": "Hello World"
            }
        ]
    }
]
```

### Remove empty strings

[](#remove-empty-strings)

```
$html = new HtmlSerializer\Html('  foo  bar   ');
$html->removeEmptyStrings(); // enabled by default
$json = $html->toJson();
```

#### Result

[](#result-4)

```
[
    {
        "node": "div",
        "children": [
            {
                "node": "p",
                "children": [
                    {
                        "node": "text",
                        "text": " foo"
                    }
                ]
            },
            {
                "node": "span",
                "children": [
                    {
                        "node": "text",
                        "text": " bar "
                    }
                ]
            }
        ]
    }
]
```

```
$html->removeEmptyStrings(false)
```

#### Result

[](#result-5)

```
[
    {
        "node": "div",
        "children": [
            {
                "node": "text",
                "text": " "
            },
            {
                "node": "p",
                "children": [
                    {
                        "node": "text",
                        "text": " foo"
                    }
                ]
            },
            {
                "node": "text",
                "text": " "
            },
            {
                "node":"span",
                "children": [
                    {
                        "node": "text",
                        "text": " bar "
                    }
                ]
            },
            {
                "node": "text",
                "text": "  "
            }
        ]
    }
]
```

###  Health Score

24

—

LowBetter than 31% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity21

Limited adoption so far

Community5

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

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.

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/544063?v=4)[M Dmitry](/maintainers/Shtrih)[@shtrih](https://github.com/shtrih)

### Embed Badge

![Health badge](/badges/shtrihstr-html-serializer/health.svg)

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

###  Alternatives

[mck89/peast

Peast is PHP library that generates AST for JavaScript code

19037.7M41](/packages/mck89-peast)[sauladam/shipment-tracker

Parses tracking information for several carriers, like UPS, USPS, DHL and GLS by simply scraping the data. No need for any kind of API access.

9642.0k](/packages/sauladam-shipment-tracker)[jstewmc/rtf

Read and write Rich Text Format (RTF) documents with PHP

46143.1k6](/packages/jstewmc-rtf)[moonshine/layouts-field

Field for repeating groups of fields for MoonShine

107.9k](/packages/moonshine-layouts-field)[tcds-io/php-jackson

A lightweight, flexible object serializer for PHP, inspired by FasterXML/jackson

112.9k10](/packages/tcds-io-php-jackson)

PHPackages © 2026

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