PHP | DOMNodeList count()函数
DOMNodeList::count()函数是PHP中的一个内置函数,用于获取列表中的节点数。
句法:
int DOMNodeList::count( void )
参数:此函数不接受任何参数。
返回值:此函数返回列表中的节点数。
下面的例子说明了PHP中的DOMNodeList::count()函数:
示例 1:
appendChild(new DOMElement('div'));
// Create a h1 element
$text1 = new DOMElement('h1', 'GeeksforGeeks');
// Create another h1 elements
$text2 = new DOMElement('h1', 'Another GeeksforGeeks');
// Append the nodes
$element->appendChild($text1);
$element->appendChild($text2);
// Get all elements with tag name 'h1'
$elements = $document->getElementsByTagName('h1');
// Count the elements
echo $elements->count();
?>
输出:
2
示例 2:
appendChild(new DOMElement('div'));
// Create a h1 element
$text1 = new DOMElement('h1', 'GeeksforGeeks');
// Create another h1 elements
$text2 = new DOMElement('h1', 'Another GeeksforGeeks');
// Append the nodes
$element->appendChild($text1);
$element->appendChild($text2);
// Get all elements with tag name 'h1'
$elements = $document->getElementsByTagName('h1');
// Count the elements
echo 'Before removing: ';
echo $elements->count() . "
";
// Remove a child
$element->removeChild($text2);
// Get all elements with tag name 'h1'
$elements = $document->getElementsByTagName('h1');
// Count the elements
echo 'After removing: ';
echo $elements->count();
?>
输出:
Before removing: 2
After removing: 1
参考: https://www. PHP.net/manual/en/domnodelist.count。 PHP