DOMAttr::isId()函数是PHP中的内置函数,用于检查属性是否为已定义的ID。根据DOM标准,它要求属性ID为类型ID。使用此功能之前,您需要使用DOMDocument::validateOnParse()方法验证您的文档。
用法:
bool DOMAttr::isId( void )
参数:此功能不接受任何参数。
返回值:如果该函数包含id属性,则该函数返回TRUE,否则返回FALSE。
下面给出的程序说明了PHP中的DOMAttr::isId()函数:
程序1:
// Create a new DOM Document
$dom = new DOMDocument('1.0', 'iso-8859-1');
// Enable validate on parse
$dom->validateOnParse = true;
// Create a div element
$element = $dom->appendChild(new DOMElement('div'));
// Create a class attribute
$attr = $element->setAttributeNode(
new DOMAttr('class', 'geekforgeeks'));
// Get the attribute
$getattr = $dom->getElementsByTagName('div')
->item(0)->getAttributeNode('class');
// Check if it is id or not
if($getattr->isId()) {
echo 'Yes, this is an id';
} else {
echo 'No, this is not an id';
}
?>
输出:
No, this is not an id
程序2:
// Create a new DOM Document
$dom = new DOMDocument('1.0', 'iso-8859-1');
// Enable validate on parse
$dom->validateOnParse = true;
// Create a div element
$element = $dom->appendChild(new DOMElement('div'));
// Create a id attribute
$attr = $element->setAttributeNode(
new DOMAttr('id', 'mynewid'));
// Set that attribute as id
$element->setIDAttribute('id', true);
// Get the attribute
$getattr = $dom->getElementsByTagName('div')
->item(0)->getAttributeNode('id');
// Check if it is id or not
if($getattr->isId()) {
echo 'Yes, this is an id';
} else {
echo 'No, this is not an id';
}
?>
输出:
Yes, this is a id