(PHP 5, PHP 7, PHP 8)
DOMDocument::loadHTML — Load HTML from a string
The function parses the HTML contained in the string source
.
Unlike loading XML, HTML does not have to be well-formed to load. This
function may also be called statically to load and create a
DOMDocument object. The static invocation may be
used when no DOMDocument properties need to be
set prior to loading.
source
The HTML string.
options
Since Libxml 2.6.0, you may also use the
options
parameter to specify additional Libxml parameters.
成功時返回 true
, 或者在失敗時返回 false
。 If called statically, returns a
DOMDocument 或者在失敗時返回 false
.
If an empty string is passed as the source
,
a warning will be generated. This warning is not generated by libxml
and cannot be handled using libxml's error handling functions.
在 PHP 8.0.0 之前可以靜態(tài)調(diào)用此方法,但會發(fā)出
E_DEPRECATED
錯誤。自 PHP 8.0.0 起,靜態(tài)調(diào)用此方法會拋出 Error 異常
盡管非正確格式化的 HTML 仍應(yīng)該被成功調(diào)入,但此函數(shù)會在遇到錯誤標記時產(chǎn)生 E_WARNING
錯誤。libxml 錯誤處理函數(shù)可以用來處理這類錯誤。
示例 #1 Creating a Document
<?php
$doc = new DOMDocument();
$doc->loadHTML("<html><body>Test<br></body></html>");
echo $doc->saveHTML();
?>