牛牛在线工具

DOMNodeList::count()

收录 8868 个 PHP 函数,含用途说明与用法示例。

DOMNodeList::count()

获取 DOMNodeList 对象中节点的数量

函数名:DOMNodeList::count() 用法:此方法用于获取 DOMNodeList 对象中节点的数量。 示例: // 创建一个新的 DOMDocument 对象 $doc = new DOMDocument(); // 加载一个 XML 文件 $doc->load('example.xml'); // 获取所有的 <book> 节点 $books = $doc->getElementsByTagName('book'); // 获取节点数量 $count = $books->count(); // 打印节点数量 echo "There are $count books in the document."; 输出: There are 3 books in the document. 解释:上述示例中,首先创建了一个 DOMDocument 对象,并使用 load() 方法加载了一个 XML 文件。然后使用 getElementsByTagName() 方法获取所有的 <book> 节点,并将返回的 DOMNodeList 对象赋值给变量 $books。接下来,使用 count() 方法获取 $books 中节点的数量,并将结果赋值给变量 $count。最后,通过输出语句打印节点数量。在这个示例中,XML 文件中存在 3 个 <book> 节点,所以最终输出的结果是 "There are 3 books in the document."。