2017 © Pedro Peláez
 

library html-serializer

Convert HTML to JSON

image

shtrihstr/html-serializer

Convert HTML to JSON

  • Thursday, August 18, 2016
  • by Shtrih
  • Repository
  • 1 Watchers
  • 5 Stars
  • 129 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 1 Versions
  • 163 % Grown

The README.md

HTML Serializer

Convert HTML to Array or JSON, (*1)

Installation

$ composer require shtrihstr/html-serializer

Usage

HTML to JSON

$html = new HtmlSerializer\Html('<div class="content"><p>Hello World</p><img src="img.png" alt="" class="image" /></div>');
$json = $html->toJson();

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

$array = $html->toArray();

Result

[
    [
        '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

$html = new HtmlSerializer\Html('<div style="color: red; background: url(img.png?foo;bar)">Hello World</div>');
$html->parseCss(); // enabled by default
$json = $html->toJson();

Result

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

```php $html->parseCss(false);, (*2)

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

Remove empty strings

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

Result

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

```php $html->removeEmptyStrings(false), (*3)

#### Result
```json
[
    {
        "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": "  "
            }
        ]
    }
]

The Versions

18/08 2016

dev-master

9999999-dev

Convert HTML to JSON

  Sources   Download

MIT

The Development Requires

by Oleksandr Strikha