CakeFest 2024: The Official CakePHP Conference

DOMDocument::loadHTMLFile

(PHP 5, PHP 7, PHP 8)

DOMDocument::loadHTMLFile ファイルから HTML を読み込む

説明

public DOMDocument::loadHTMLFile(string $filename, int $options = 0): bool

この関数は、filename という名前のファイルから 読み込んだ HTML ドキュメントをパースします。 XML を読み込む場合とは異なり、妥当な HTML でなくても 読み込むことができます。

警告

この関数は、HTML4 のパーサを使って入力をパースします。モダンなWebブラウザが採用している HTML5 のパースルールとは異なります。入力によっては、このことが原因で異なるDOM構造になるかもしれません。よって、この関数はHTMLを無害化する目的で安全に使えません。

例を挙げましょう。HTML要素によっては、暗黙のうちに親の要素を閉じるものがあります。親要素を自動で閉じるルールは、HTML4 と HTML5 で異なります。よって、DOMDocument が表す DOM 構造は Webブラウザ上のそれと異なる可能性があります。このことから、攻撃者がHTMLを壊す攻撃を許す可能性があります。

パラメータ

filename

HTML ファイルへのパス。

options

追加の Libxml パラメータ を、ビット演算子の OR で指定します。

戻り値

成功した場合に true を、失敗した場合に false を返します。

エラー / 例外

空の文字列を filename に渡したり中身が空のファイルを指定したりすると、警告が発生します。 この警告は libxml が発するものではないので、libxml のエラー処理関数 では処理できません。

壊れた HTML も読み込めますが、マークアップが正しくない場合には E_WARNING が発生します。 このエラーの処理には libxml のエラー処理関数 が使えます。

変更履歴

バージョン 説明
8.3.0 この関数の仮の戻り値の型が、bool になりました。+
8.0.0 この関数を static メソッドとしてコールすると、 Error がスローされるようになりました。 これより前のバージョンでは、 E_DEPRECATED が発生していました。

例1 ドキュメントを作成する

<?php
$doc
= new DOMDocument();
$doc->loadHTMLFile("filename.html");
echo
$doc->saveHTML();
?>

参考

add a note

User Contributed Notes 6 notes

up
13
onemanbanddan at gmail dot com
10 years ago
The options for surpressing errors and warnings will not work with this as they do for loadXML()
e.g.
<?php
$doc
->loadHTMLFile($file, LIBXML_NOWARNING | LIBXML_NOERROR);
?>
will not work.
you must use:
<?php
libxml_use_internal_errors
(true);
$doc->loadHTMLFile($file);
?>
and handle the exceptions as neccesarry.
up
3
Mark Omohundro, ajamyajax dot com
15 years ago
<?php
// try this html listing example for all nodes / includes a few getElementsByTagName options:

$file = $DOCUMENT_ROOT. "test.html";
$doc = new DOMDocument();
$doc->loadHTMLFile($file);

// example 1:
$elements = $doc->getElementsByTagName('*');
// example 2:
$elements = $doc->getElementsByTagName('html');
// example 3:
//$elements = $doc->getElementsByTagName('body');
// example 4:
//$elements = $doc->getElementsByTagName('table');
// example 5:
//$elements = $doc->getElementsByTagName('div');

if (!is_null($elements)) {
foreach (
$elements as $element) {
echo
"<br/>". $element->nodeName. ": ";

$nodes = $element->childNodes;
foreach (
$nodes as $node) {
echo
$node->nodeValue. "\n";
}
}
}
?>
up
-2
qrworld.net
9 years ago
In this post http://softontherocks.blogspot.com/2014/11/descargar-el-contenido-de-una-url_11.html I found a simple way to get the content of a URL with DOMDocument, loadHTMLFile and saveHTML().

function getURLContent($url){
$doc = new DOMDocument;
$doc->preserveWhiteSpace = FALSE;
@$doc->loadHTMLFile($url);
return $doc->saveHTML();
}
up
-4
andy at carobert dot com
18 years ago
This puts the HTML into a DOM object which can be parsed by individual tags, attributes, etc.. Here is an example of getting all the 'href' attributes and corresponding node values out of the 'a' tag. Very cool....

<?php
$myhtml
= <<<EOF
<html>
<head>
<title>My Page</title>
</head>
<body>
<p><a href="/mypage1">Hello World!</a></p>
<p><a href="/mypage2">Another Hello World!</a></p>
</body>
</html>
EOF;

$doc = new DOMDocument();
$doc->loadHTML($myhtml);

$tags = $doc->getElementsByTagName('a');

foreach (
$tags as $tag) {
echo
$tag->getAttribute('href').' | '.$tag->nodeValue."\n";
}
?>

This should output:

/mypage1 | Hello World!
/mypage2 | Another Hello World!
up
-17
gzech at SPAMFILTER dot eso dot org
17 years ago
If you want to suppress output warnings from loadHTMLFile($url), put an @ sign in front. This even works in:
<?php
$load
= @$dom->loadHTMLFile($url);
?>
up
-19
bens at effortlessis dot com
18 years ago
Note that this function doesn't parse the individual tags WITHIN the html file - it's all loaded as a "black box", and you end up with an XML widget that comprises nothing but the complete chunk of HTML.

I was hoping it would function as a sort of HTML-validator/parser, but that doesn't seem to be the case.
To Top