Home

Awesome

UKRAINE NEEDS YOUR HELP NOW!

On 24 February 2022, Russian President Vladimir Putin ordered an invasion of Ukraine by Russian Armed Forces.

Your support is urgently needed.

THANK YOU!


HTML5-PHP

HTML5 is a standards-compliant HTML5 parser and writer written entirely in PHP. It is stable and used in many production websites, and has well over five million downloads.

HTML5 provides the following features.

CI Latest Stable Version Code Coverage Scrutinizer Code Quality Stability: Sustained

Installation

Install HTML5-PHP using composer.

By adding the masterminds/html5 dependency to your composer.json file:

{
  "require" : {
    "masterminds/html5": "^2.0"
  },
}

By invoking require command via composer executable:

composer require masterminds/html5

Basic Usage

HTML5-PHP has a high-level API and a low-level API.

Here is how you use the high-level HTML5 library API:

<?php
// Assuming you installed from Composer:
require "vendor/autoload.php";

use Masterminds\HTML5;

// An example HTML document:
$html = <<< 'HERE'
  <html>
  <head>
    <title>TEST</title>
  </head>
  <body id='foo'>
    <h1>Hello World</h1>
    <p>This is a test of the HTML5 parser.</p>
  </body>
  </html>
HERE;

// Parse the document. $dom is a DOMDocument.
$html5 = new HTML5();
$dom = $html5->loadHTML($html);

// Render it as HTML5:
print $html5->saveHTML($dom);

// Or save it to a file:
$html5->save($dom, 'out.html');

The $dom created by the parser is a full DOMDocument object. And the save() and saveHTML() methods will take any DOMDocument.

Options

It is possible to pass in an array of configuration options when loading an HTML5 document.

// An associative array of options
$options = array(
  'option_name' => 'option_value',
);

// Provide the options to the constructor
$html5 = new HTML5($options);

$dom = $html5->loadHTML($html);

The following options are supported:

The Low-Level API

This library provides the following low-level APIs that you can use to create more customized HTML5 tools:

The unit tests exercise each piece of the API, and every public function is well-documented.

Parser Design

The parser is designed as follows:

Serializer Design

The serializer takes a data structure (the DOMDocument) and transforms it into a character representation -- an HTML5 document.

The serializer is broken into three parts:

The serializer (save(), saveHTML()) follows the section 8.9 of the HTML 5.0 spec. So tags are serialized according to these rules:

Known Issues (Or, Things We Designed Against the Spec)

Please check the issue queue for a full list, but the following are issues known issues that are not presently on the roadmap:

XML Namespaces

To use XML style namespaces you have to configure well the main HTML5 instance.

use Masterminds\HTML5;
$html = new HTML5(array(
    "xmlNamespaces" => true
));

$dom = $html->loadHTML('<t:tag xmlns:t="http://www.example.com"/>');

$dom->documentElement->namespaceURI; // http://www.example.com

You can also add some default prefixes that will not require the namespace declaration, but its elements will be namespaced.

use Masterminds\HTML5;
$html = new HTML5(array(
    "implicitNamespaces"=>array(
        "t"=>"http://www.example.com"
    )
));

$dom = $html->loadHTML('<t:tag/>');

$dom->documentElement->namespaceURI; // http://www.example.com

Thanks to...

The dedicated (and patient) contributors of patches small and large, who have already made this library better.See the CREDITS file for a list of contributors.

We owe a huge debt of gratitude to the original authors of html5lib.

While not much of the original parser remains, we learned a lot from reading the html5lib library. And some pieces remain here. In particular, much of the UTF-8 and Unicode handling is derived from the html5lib project.

License

This software is released under the MIT license. The original html5lib library was also released under the MIT license.

See LICENSE.txt

Certain files contain copyright assertions by specific individuals involved with html5lib. Those have been retained where appropriate.