phpday 2025 - Call For Papers

DOMElement::removeAttributeNode

(PHP 5, PHP 7, PHP 8)

DOMElement::removeAttributeNodeRemove atributo

Descrição

public DOMElement::removeAttributeNode(DOMAttr $attr): DOMAttr|false

Remove o atributo attr do elemento.

Parâmetros

attr

O nó do atributo.

Valor Retornado

Retorna true em caso de sucesso ou false em caso de falha.

Erros/Exceções

DOM_NO_MODIFICATION_ALLOWED_ERR

Gerado se o nó for somente leitura.

DOM_NOT_FOUND_ERR

Gerado se attr não for um atributo do elemento.

Veja Também

adicione uma nota

Notas Enviadas por Usuários (em inglês) 1 note

up
3
xr07354 at gmx dot de
11 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