DOMElement::hasAttribute()函數是PHP中的內置函數,用於了解具有特定名稱的屬性是否作為元素的成員存在。
用法:
bool DOMElement::hasAttribute( string $name )
參數:該函數接受單個參數$name,該參數保存屬性的名稱。
返回值:如果成功,則此函數返回TRUE;如果失敗,則返回FALSE。
下麵給出的程序說明了PHP中的DOMElement::hasAttribute()函數:
程序1:
<?php
// Create a new DOMDocument
$dom = new DOMDocument();
// Load the XML
$dom->loadXML("<?xml version=\"1.0\"?>
<root>
<div>
<!-- id attribute is there -->
<p id=\"prog\"> HELLO </p>
</div>
</root>");
// Get the elements
$nodeList = $dom->getElementsByTagName('p');
foreach ($nodeList as $node) {
if($node->hasAttribute('id')) {
echo "Yes, id attribute is there.";
}
}
?>
輸出:
Yes, id attribute is there.
程序2:
<?php
// Create a new DOMDocument
$dom = new DOMDocument();
// Load the XML
$dom->loadXML("<?xml version=\"1.0\"?>
<root>
<div>
<!-- id attribute is missing -->
<p> HELLO </p>
</div>
</root>");
// Get the elements
$nodeList = $dom->getElementsByTagName('p');
foreach ($nodeList as $node) {
if(!$node->hasAttribute('id')) {
echo "No, id attribute isn't there.";
}
}
?>
輸出:
No, id attribute isn't there.
參考: https://www.php.net/manual/en/domelement.hasattribute.php
相關用法
- PHP DOMElement getAttributeNodeNS()用法及代碼示例
- PHP DOMElement getAttribute()用法及代碼示例
- PHP DOMElement setAttribute()用法及代碼示例
- PHP DOMElement setAttributeNode()用法及代碼示例
- PHP DOMElement removeAttributeNS()用法及代碼示例
- PHP DOMElement removeAttribute()用法及代碼示例
- PHP DOMElement getAttributeNS()用法及代碼示例
- PHP DOMElement getElementsByTagName()用法及代碼示例
- PHP DOMElement hasAttributeNS()用法及代碼示例
- PHP DOMElement __construct()用法及代碼示例
- PHP DOMElement setIdAttributeNode()用法及代碼示例
- PHP DOMElement setIdAttributeNS()用法及代碼示例
- PHP DOMElement getElementsByTagNameNS()用法及代碼示例
- PHP DOMElement setIdAttribute()用法及代碼示例
- PHP DOMElement getAttributeNode()用法及代碼示例
注:本文由純淨天空篩選整理自gurrrung大神的英文原創作品 PHP | DOMElement hasAttribute() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。