Skip to content
Matt Farina edited this page Aug 17, 2013 · 6 revisions

The typical uses of html5-php are to parse html5 to a DOM or to turn a DOM into html5.

Parsing

The three ways to easily parse html5 are html5 strings, html5 files, and html5 fragments.

Parsing html5 strings

// 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.
$dom = \HTML5::loadHTML($html);

DOMDocument is the same object returned when parsing html4, xml, and xhtml with the built in tools from libxml.

Parsing html5 files

Parsing a file or resource can happen without loading the markup to a string.

// Parse the document. $dom is a DOMDocument.
$dom = \HTML5::loadHTMLFile('path/to/file.html');

Parsing html5 fragments

// An example HTML fragment:
$fragment = "<p>This is a test of the HTML5 parser.</p>";

// Parse the document. $dom is a DOMDocumentFragment.
$dom = \HTML5::loadHTMLFragment($fragment);

DOMDocumentFragment is similar to DOMDocument in that it is a container for elements. DOMDocumentFragments can be attached to DOMDocuments. When that happens all the children are moved to the DOMDocument.

Serializing (Writing)

The serializer can write DOMDocuments and DOMDocumentFragments to strings and files.

Writing to a string

// $dom is either a DOMDocument, DOMDocumentFragment, or DOMNodeList.
$string = \HTML5::saveHTML($dom);

Writing to a file

// $dom is either a DOMDocument, DOMDocumentFragment, or DOMNodeList.
$string = \HTML5::save($dom, 'path/to/file.html');
Clone this wiki locally