DOMElement::removeAttribute
(PHP 5, PHP 7, PHP 8)
DOMElement::removeAttribute — Removes attribute
说明
public DOMElement::removeAttribute(string
$qualifiedName
): bool
Removes attribute named qualifiedName
from the element.
参数
-
qualifiedName
-
The name of the attribute.
返回值
成功时返回 true
, 或者在失败时返回 false
。
错误/异常
-
DOM_NO_MODIFICATION_ALLOWED_ERR
-
Raised if the node is readonly.
参见
- DOMElement::hasAttribute() - Checks to see if attribute exists
- DOMElement::getAttribute() - Returns value of attribute
- DOMElement::setAttribute() - Adds new or modifies existing attribute

User Contributed Notes 2 notes
Rakesh Verma - rakeshnsony at gmail dot com ¶
11 years ago
<?php
//Store your html into $html variable.
$html="<html>
<head>
<title>Rakesh Verma</title>
</head>
<body>
<a href='http://example.com'>Example</a>
<a href='http://google.com'>Google</a>
<a href='http://www.yahoo.com'>Yahoo</a>
</body>
</html>";
$dom = new DOMDocument();
$dom->loadHTML($html);
//Evaluate Anchor tag in HTML
$xpath = new DOMXPath($dom);
$hrefs = $xpath->evaluate("/html/body//a");
for ($i = 0; $i < $hrefs->length; $i++) {
$href = $hrefs->item($i);
$url = $href->getAttribute('href');
//remove and set target attribute
$href->removeAttribute('target');
$href->setAttribute("target", "_blank");
$newURL=$url.".au";
//remove and set href attribute
$href->removeAttribute('href');
$href->setAttribute("href", $newURL);
}
// save html
$html=$dom->saveHTML();
echo $html;
?>
suwayan at mail dot ru ¶
10 years ago
<?php
/*When I try to get a some attribute from not validated HTML or XML document, PHP dies with no errors in logs or output:
*/
function is_attribute_value($obj,$type,$value)
{
$_ret=false;
if($obj)
{
if($val=$obj->getAttribute($type))
{
if($val==$value)
{
$_ret=true;
}
}
}
return $_ret;
}
//And this check helped to me:
function is_attribute_value($obj,$type,$value)
{
$_ret=false;
if($obj->attributes)
{
if($val=$obj->getAttribute($type))
{
if($val==$value)
{
$_ret=true;
}
}
}
return $_ret;
}
?>
备份地址:http://www.lvesu.com/blog/php/domelement.removeattribute.php