class HTML5 (View source)

This class offers convenience methods for parsing and serializing HTML5.

It is roughly designed to mirror the \DOMDocument class that is provided with most versions of PHP.

EXPERIMENTAL. This may change or be completely replaced.

Methods

__construct( array $options = array())

No description

array
getOptions()

Get the default options.

load( string $file, array $options = array())

Load and parse an HTML file.

loadHTML( string $string, array $options = array())

Parse a HTML Document from a string.

loadHTMLFile( string $file, array $options = array())

Convenience function to load an HTML file.

loadHTMLFragment( string $string, array $options = array())

Parse a HTML fragment from a string.

array
getErrors()

Return all errors encountered into parsing phase

bool
hasErrors()

Return true it some errors were encountered into parsing phase

parse( InputStream $input, array $options = array())

Parse an input stream.

parseFragment( InputStream $input, array $options = array())

Parse an input stream where the stream is a fragment.

save( mixed $dom, string $file, array $options = array())

Save a DOM into a given file as HTML5.

string
saveHTML( mixed $dom, array $options = array())

Convert a DOM into an HTML5 string.

Details

at line line 34
__construct( array $options = array())

Parameters

array $options

at line line 44
array getOptions()

Get the default options.

Return Value

array The default options.

at line line 68
DOMDocument load( string $file, array $options = array())

Load and parse an HTML file.

This will apply the HTML5 parser, which is tolerant of many varieties of HTML, including XHTML 1, HTML 4, and well-formed HTML 3. Note that in these cases, not all of the old data will be preserved. For example, XHTML's XML declaration will be removed.

The rules governing parsing are set out in the HTML 5 spec.

Parameters

string $file The path to the file to parse. If this is a resource, it is assumed to be an open stream whose pointer is set to the first byte of input.
array $options Configuration options when parsing the HTML

Return Value

DOMDocument A DOM document. These object type is defined by the libxml library, and should have been included with your version of PHP.

at line line 94
DOMDocument loadHTML( string $string, array $options = array())

Parse a HTML Document from a string.

Take a string of HTML 5 (or earlier) and parse it into a DOMDocument.

Parameters

string $string A html5 document as a string.
array $options Configuration options when parsing the HTML

Return Value

DOMDocument A DOM document. DOM is part of libxml, which is included with almost all distribtions of PHP.

at line line 117
DOMDocument loadHTMLFile( string $file, array $options = array())

Convenience function to load an HTML file.

This is here to provide backwards compatibility with the PHP DOM implementation. It simply calls load().

Parameters

string $file The path to the file to parse. If this is a resource, it is assumed to be an open stream whose pointer is set to the first byte of input.
array $options Configuration options when parsing the HTML

Return Value

DOMDocument A DOM document. These object type is defined by the libxml library, and should have been included with your version of PHP.

at line line 133
DOMDocumentFragment loadHTMLFragment( string $string, array $options = array())

Parse a HTML fragment from a string.

Parameters

string $string The html5 fragment as a string.
array $options Configuration options when parsing the HTML

Return Value

DOMDocumentFragment A DOM fragment. The DOM is part of libxml, which is included with almost all distributions of PHP.

at line line 145
array getErrors()

Return all errors encountered into parsing phase

Return Value

array

at line line 155
bool hasErrors()

Return true it some errors were encountered into parsing phase

Return Value

bool

at line line 166
parse( InputStream $input, array $options = array())

Parse an input stream.

Lower-level loading function. This requires an input stream instead of a string, file, or resource.

Parameters

InputStream $input
array $options

at line line 185
parseFragment( InputStream $input, array $options = array())

Parse an input stream where the stream is a fragment.

Lower-level loading function. This requires an input stream instead of a string, file, or resource.

Parameters

InputStream $input
array $options

at line line 210
save( mixed $dom, string $file, array $options = array())

Save a DOM into a given file as HTML5.

Parameters

mixed $dom The DOM to be serialized.
string $file The filename to be written.
array $options Configuration options when serializing the DOM. These include: - encode_entities: Text written to the output is escaped by default and not all entities are encoded. If this is set to true all entities will be encoded. Defaults to false.

at line line 243
string saveHTML( mixed $dom, array $options = array())

Convert a DOM into an HTML5 string.

Parameters

mixed $dom The DOM to be serialized.
array $options Configuration options when serializing the DOM. These include: - encode_entities: Text written to the output is escaped by default and not all entities are encoded. If this is set to true all entities will be encoded. Defaults to false.

Return Value

string A HTML5 documented generated from the DOM.