大网数据广告活动
PHP 语言与运行手册

Dom\HTMLDocument::createFromString · Parses an HTML document from a string

· 52CE 我爱测

Dom\HTMLDocument::createFromString — Parses an HTML document from a string

Dom\HTMLDocument::createFromString

(PHP 8 >= 8.4.0)

Dom\HTMLDocument::createFromString — Parses an HTML document from a string

说明

public static function Dom\HTMLDocument::createFromString(string $source, int $options = 0, ?string $overrideEncoding = null): Dom\HTMLDocument

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

参数

source

The string containing the HTML 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.

返回值

The parsed document as an Dom\HTMLDocument instance.

错误/异常

  • Throws a ValueError if options contains an invalid option.

  • Throws a ValueError if overrideEncoding is an unknown encoding.

示例

示例 #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();
?>

以上示例会输出:

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

</body></html>

注释

注意:

Whitespace in the html and head tags is not considered significant and may lose formatting.

参见

来源:PHP Documentation Group · 作者与贡献记录 · CC BY 3.0。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
PHP Documentation Group
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。