PHP 5.5.15 is released

DOMElement::removeAttributeNode

(PHP 5)

DOMElement::removeAttributeNodeElimina un atributo

Descripción

public bool DOMElement::removeAttributeNode ( DOMAttr $oldnode )

Elimina el atributo oldnode del elemento.

Parámetros

oldnode

El nodo atributo.

Valores devueltos

Devuelve TRUE en caso de éxito o FALSE en caso de error.

Errores/Excepciones

DOM_NO_MODIFICATION_ALLOWED_ERR

Lanzado si el nodo es de sólo lectura.

DOM_NOT_FOUND_ERROR

Lanzado si oldnode no es un atributo del elemento.

Ver también

add a note add a note

User Contributed Notes 1 note

up
0
xr07354 at gmx dot de
1 year 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