PHP 8.4.3 Released!

Dom\HTMLDocument::createFromString

(PHP 8 >= 8.4.0)

Dom\HTMLDocument::createFromStringParses an HTML document from a string

Опис

public static 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

Побітове АБО констант опцій libxml.

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.

Прогляньте також

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top