Documentation

HTML5
in package

This class offers convenience methods for parsing and serializing HTML5.

It is roughly designed to mirror the \DOMDocument native class.

Table of Contents

Properties

$errors  : mixed
$defaultOptions  : array<string|int, mixed>
Global options for the parser and serializer.

Methods

__construct()  : mixed
getErrors()  : array<string|int, mixed>
Return all errors encountered into parsing phase.
getOptions()  : array<string|int, mixed>
Get the current default options.
hasErrors()  : bool
Return true it some errors were encountered into parsing phase.
load()  : DOMDocument
Load and parse an HTML file.
loadHTML()  : DOMDocument
Parse a HTML Document from a string.
loadHTMLFile()  : DOMDocument
Convenience function to load an HTML file.
loadHTMLFragment()  : DOMDocumentFragment
Parse a HTML fragment from a string.
parse()  : DOMDocument
Parse an input string.
parseFragment()  : DOMDocumentFragment
Parse an input stream where the stream is a fragment.
save()  : mixed
Save a DOM into a given file as HTML5.
saveHTML()  : string
Convert a DOM into an HTML5 string.

Properties

$errors

protected mixed $errors = array()

$defaultOptions

Global options for the parser and serializer.

private array<string|int, mixed> $defaultOptions = array( // Whether the serializer should aggressively encode all characters as entities. 'encode_entities' => false, // Prevents the parser from automatically assigning the HTML5 namespace to the DOM document. 'disable_html_ns' => false, )

Methods

__construct()

public __construct([array<string|int, mixed> $defaultOptions = array() ]) : mixed
Parameters
$defaultOptions : array<string|int, mixed> = array()

getErrors()

Return all errors encountered into parsing phase.

public getErrors() : array<string|int, mixed>
Return values
array<string|int, mixed>

getOptions()

Get the current default options.

public getOptions() : array<string|int, mixed>
Return values
array<string|int, mixed>

hasErrors()

Return true it some errors were encountered into parsing phase.

public hasErrors() : bool
Return values
bool

load()

Load and parse an HTML file.

public load(string|resource $file[, array<string|int, mixed> $options = array() ]) : DOMDocument

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
$file : string|resource

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.

$options : array<string|int, mixed> = array()

Configuration options when parsing the HTML.

Return values
DOMDocument

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

loadHTML()

Parse a HTML Document from a string.

public loadHTML(string $string[, array<string|int, mixed> $options = array() ]) : DOMDocument

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

Parameters
$string : string

A html5 document as a string.

$options : array<string|int, mixed> = array()

Configuration options when parsing the HTML.

Return values
DOMDocument

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

loadHTMLFile()

Convenience function to load an HTML file.

public loadHTMLFile(string $file[, array<string|int, mixed> $options = array() ]) : DOMDocument

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

Parameters
$file : string

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.

$options : array<string|int, mixed> = array()

Configuration options when parsing the HTML.

Return values
DOMDocument

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

loadHTMLFragment()

Parse a HTML fragment from a string.

public loadHTMLFragment(string $string[, array<string|int, mixed> $options = array() ]) : DOMDocumentFragment
Parameters
$string : string

the HTML5 fragment as a string

$options : array<string|int, mixed> = array()

Configuration options when parsing the HTML

Return values
DOMDocumentFragment

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

parse()

Parse an input string.

public parse(string $input[, array<string|int, mixed> $options = array() ]) : DOMDocument
Parameters
$input : string
$options : array<string|int, mixed> = array()
Return values
DOMDocument

parseFragment()

Parse an input stream where the stream is a fragment.

public parseFragment(string $input[, array<string|int, mixed> $options = array() ]) : DOMDocumentFragment

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

Parameters
$input : string

The input data to parse in the form of a string.

$options : array<string|int, mixed> = array()

An array of options.

Return values
DOMDocumentFragment

save()

Save a DOM into a given file as HTML5.

public save(mixed $dom, string|resource $file[, array<string|int, mixed> $options = array() ]) : mixed
Parameters
$dom : mixed

The DOM to be serialized.

$file : string|resource

The filename to be written or resource to write to.

$options : array<string|int, mixed> = array()

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.

saveHTML()

Convert a DOM into an HTML5 string.

public saveHTML(mixed $dom[, array<string|int, mixed> $options = array() ]) : string
Parameters
$dom : mixed

The DOM to be serialized.

$options : array<string|int, mixed> = array()

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 values
string

A HTML5 documented generated from the DOM.


        
On this page

Search results