📜  PHP | DOMNamedNodeMap count()函数(1)

📅  最后修改于: 2023-12-03 15:03:36.874000             🧑  作者: Mango

PHP | DOMNamedNodeMap count()函数

简介

DOMNamedNodeMap count() 函数用于返回 DOMNamedNodeMap 对象中节点的数量。

语法
int DOMNamedNodeMap::count ( void )
参数

该函数没有参数。

返回值

返回值为 DOMNamedNodeMap 对象中节点的数量。

示例
<?php
$xmlStr = "<bookstore><book><title>PHP Cookbook</title></book><book><title>JavaScript Cookbook</title></book></bookstore>";
$doc = new DOMDocument();
$doc->loadXML($xmlStr);
$books = $doc->getElementsByTagName("book");
foreach ($books as $book) {
  $attributes = $book->attributes;
  echo "The book " . $book->getElementsByTagName("title")[0]->nodeValue . " has " . $attributes->count() . " attribute(s).<br>";
}
?>

以上代码输出结果为:

The book PHP Cookbook has 0 attribute(s).
The book JavaScript Cookbook has 0 attribute(s).
参考链接