使用 PHP DOMDocument,如何找到加载的最大深度 HTML?
Using PHP DOMDocument, how do you find the maximum depth of loaded HTML?
使用 PHP DOM 文档 class 加载了有效的 HTML 文档:
https://www.php.net/manual/en/class.domdocument.php
如何计算出 DOM 树达到的最大深度?
一种计算方法是使用 DOMXPath
,从查询 *
开始,然后添加 /*
直到查询 returns 没有节点:
$doc = new DOMDocument();
$doc->loadHTML($html);
$xpath = new DOMXPath($doc);
$query = '*';
$depth = 1;
while ($xpath->query($query)->length) {
$depth++;
$query .= "/*";
}
echo "maximum depth = $depth\n";
使用 PHP DOM 文档 class 加载了有效的 HTML 文档: https://www.php.net/manual/en/class.domdocument.php
如何计算出 DOM 树达到的最大深度?
一种计算方法是使用 DOMXPath
,从查询 *
开始,然后添加 /*
直到查询 returns 没有节点:
$doc = new DOMDocument();
$doc->loadHTML($html);
$xpath = new DOMXPath($doc);
$query = '*';
$depth = 1;
while ($xpath->query($query)->length) {
$depth++;
$query .= "/*";
}
echo "maximum depth = $depth\n";