DOMElement::removeAttributeNode

(PHP 5, PHP 7)

DOMElement::removeAttributeNode属性を削除する

説明

public bool DOMElement::removeAttributeNode ( DOMAttr $oldnode )

属性 oldnode を要素から削除します。

パラメータ

oldnode

属性ノード。

返り値

成功した場合に TRUE を、失敗した場合に FALSE を返します。

エラー / 例外

DOM_NO_MODIFICATION_ALLOWED_ERR

ノードが読み込み専用の場合に発生します。

DOM_NOT_FOUND_ERROR

oldnode が要素の属性でない場合に発生します。

参考

add a note add a note

User Contributed Notes 1 note

up
1
xr07354 at gmx dot de
4 years ago
Basic: I use PHP5.4.9 from Ubuntu 13.04 repository. The aim of my code is to iterate HTML source (as a DomDocument) recursively and cleanup everything that is not valid to be used inside Epub files (i.e. and attribute align is not valid for paragraphs in Epubs).

FIRST: Today I tried removing attributes from a DOMElement using this simple code:
<?php
for ( $k=0; $k < $element->attributes->length; $k++) {
    if(
/* some rule */ ){
       
var_dump( $element->attributes->item($k)->nodeName);
       
$element->removeAttributeNode( $element->attributes->item($k));
    }
}
?>
Unfortunately all attributes still existed when this loop was finished, even if these var_dumps told me that deleting them was tried.

I solved this problem iterating the attributes list backward:
<?php
for ( $k = $element->attributes->length - 1; $k >= 0; --$k) {
    if(
/* same rule */ ){
       
var_dump( $element->attributes->item($k)->nodeName);
       
$element->removeAttributeNode( $element->attributes->item($k));
    }
}
?>
SECOND: DOMElement::removeAttributeNode does NOT return a bool but a DOMAttr object.
To Top