Dom\HTMLDocument::createFromFile
(PHP 8 >= 8.4.0)
Dom\HTMLDocument::createFromFile — Parses an HTML document from a file
Description
$path, int $options = 0, ?string $overrideEncoding = null): Dom\HTMLDocumentParses an HTML document from a file, according to the living standard.
Parameters
path
The path to the file to parse.
options
Bitwise OR
of the libxml option constants.
It is also possible to pass Dom\HTML_NO_DEFAULT_NS
to 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
pathcontains null bytes or contains"%00". -
Throws a ValueError if
optionscontains an invalid option. -
Throws a ValueError if
overrideEncodingis an unknown encoding. -
Throws an Exception if the file could not be opened.
Examples
Example #1 Dom\HTMLDocument::createFromFile() example
Parses a document from a file.
<?php
$dom = Dom\HTMLDocument::createFromFile('/path/to/file.html');
echo $dom->saveHtml();
?>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::createFromString() - Parses an HTML document from a string
来源:PHP Documentation Group · 作者与贡献记录 · CC BY 3.0。离线整理了版式、链接和媒体显示。
