-
Notifications
You must be signed in to change notification settings - Fork 119
Basic Usage
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.
The three ways to easily parse html5 are html5 strings, html5 files, and html5 fragments.
// 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 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');
// 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.
The serializer can write DOMDocuments and DOMDocumentFragments to strings and files.
// $dom is either a DOMDocument, DOMDocumentFragment, or DOMNodeList.
$string = \HTML5::saveHTML($dom);
// $dom is either a DOMDocument, DOMDocumentFragment, or DOMNodeList.
$string = \HTML5::save($dom, 'path/to/file.html');