Dom\HTMLDocument::createFromString
(PHP 8 >= 8.4.0)
Dom\HTMLDocument::createFromString — Parses an HTML document from a string
Description
public static Dom\HTMLDocument::createFromString(string
$source, int $options = 0, ?string $overrideEncoding = null): Dom\HTMLDocumentParses an HTML document from a string, according to the living standard.
Parameters
source- The string containing the HTML to parse.
optionsBitwise
It is also possible to passORof the libxml option constants.Dom\HTML_NO_DEFAULT_NSto disable the use of the HTML namespace and the template element. This should only be used if the implications are properly understood.overrideEncoding- The encoding that the document was created in. If not provided, it will attempt to determine the encoding that is most likely used.
Return Values
The parsed document as an Dom\HTMLDocument instance.
Errors/Exceptions
- Throws a ValueError if
optionscontains an invalid option. - Throws a ValueError if
overrideEncodingis an unknown encoding.
Examples
Example #1 Dom\HTMLDocument::createFromString() example
Parses a sample document.
<?php
$dom = Dom\HTMLDocument::createFromString(<<<'HTML'
<!DOCTYPE html>
<html>
<body>
<p>Hello, world!</p>
</body>
</html>
HTML);
echo $dom->saveHtml();
?>The above example will output:
<!DOCTYPE html><html><head></head><body>
<p>Hello, world!</p>
</body></html>Notes
Note: Whitespace in the
htmlandheadtags is not considered significant and may lose formatting.
See Also
- Dom\HTMLDocument::createEmpty() - Creates an empty HTML document
- Dom\HTMLDocument::createFromFile() - Parses an HTML document from a file
↑ and ↓ to navigate • Enter to select • Esc to close • / to open