PHP | DOMNamedNodeMap item()函数
DOMNamedNodeMap::item()函数是PHP中的一个内置函数,用于检索由索引指定的节点。此函数用于从元素中获取特定属性,进一步我们可以根据要求获取该属性的名称或值。
句法:
DOMNamedNodeMap DOMNamedNodeMap::item( int $index )
参数:此函数接受一个保存索引的参数$ index。
返回值:此函数返回地图中第 index 个位置的节点。
下面给出的程序说明了PHP中的DOMNamedNodeMap::item()函数:
程序 1:在这个例子中,我们将使用 index() 获取属性后获取它们的名称
loadXML("
Geeksforgeeks
");
// Get the elements
$node = $dom->getElementsByTagName('h1')[0];
// Get the 2nd attribute's value
$attribute1 = $node->attributes->item(0)->nodeName;
$attribute2 = $node->attributes->item(1)->nodeName;
$attribute3 = $node->attributes->item(2)->nodeName;
echo "$attribute1, $attribute2 and $attribute3";
?>
输出:
id, class and attrib
程序 2:在本例中,我们将使用 index() 获取属性后获取属性的值
loadXML("
Geeksforgeeks
");
// Get the elements
$node = $dom->getElementsByTagName('h1')[0];
// Get the 2nd attribute's value
$attribute1 = $node->attributes->item(0)->nodeValue;
$attribute2 = $node->attributes->item(1)->nodeValue;
$attribute3 = $node->attributes->item(2)->nodeValue;
echo "$attribute1, $attribute2 and $attribute3";
?>
输出:
first, geeksforgeeks and attrib_value
参考: https://www. PHP.net/manual/en/domnamednodemap.item。 PHP