查找PHP,DOM,XPath中的节点数 [英] Finding number of nodes in PHP, DOM, XPath
问题描述
我目前有这个肮脏的解决方案:
$ i = 0;
foreach($ nodes as $ node){
echo $ dom-> savexml($ nodes-> item($ i));
$ i ++;
}
有没有一个更清洁的解决方案来查找节点数,我已经尝试了(),但这不起作用。
您尚未发布任何与 $节点
所以我假设你正在使用DOMXPath和query(),或者至少有一个 DOMNodeList 。
DOMXPath :: query()返回一个具有长度成员的DOMNodeList。您可以通过(给出您的代码)访问它:
$ nodes-> length
I am loading HTML into DOM and then querying it using XPath in PHP. My current problem is how do I find out how many matches have been made, and once that is ascertained, how do I access them?
I currently have this dirty solution:
$i = 0;
foreach($nodes as $node) {
echo $dom->savexml($nodes->item($i));
$i++;
}
Is there a cleaner solution to find the number of nodes, I have tried count(), but that does not work.
You haven't posted any code related to $nodes
so I assume you are using DOMXPath and query(), or at the very least, you have a DOMNodeList.
DOMXPath::query() returns a DOMNodeList, which has a length member. You can access it via (given your code):
$nodes->length
这篇关于查找PHP,DOM,XPath中的节点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!