php8.5
Home/ Manual/ dom / htmldocument/ Dom\HTMLDocument::createFromString

Dom\HTMLDocument::createFromString

PHP function Edit on GitHub ✎

(PHP 8 >= 8.4.0)

Parses an HTML document from a string

Description

Dom\HTMLDocument::createFromString(string $source, int $options = 0, string|null $overrideEncoding = null): Dom\HTMLDocument

Parses an HTML document from a string, according to the living standard.

Parameters

source

The string containing the HTML to parse.

options

Options

overrideEncoding

Encoding

Return Values

The parsed document as an Dom\HTMLDocument instance.

Errors/Exceptions

Examples

Dom\HTMLDocument::createFromString example

Parses a sample document.

php
<?php
$dom = Dom\HTMLDocument::createFromString(<<<'HTML'

<html>
<body>
    <p>Hello, world!</p>
</body>
</html>
HTML);
echo $dom->saveHtml();
?>

The above example will output:

output
<!DOCTYPE html><html><head></head><body>
    <p>Hello, world!</p>

</body></html>

Whitespace

See Also

  • Dom\HTMLDocument::createEmpty
  • Dom\HTMLDocument::createFromFile

Source: reference/dom/dom/htmldocument/createfromstring.xml · from the official PHP manual (php/doc-en)