downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

DomNode->replace_node> <DomNode->remove_child
Last updated: Fri, 20 May 2011

view this page in

DomNode->replace_child

(PHP 4 >= 4.2.0)

DomNode->replace_child Reemplaza un hijo

Descripción

domelement DomNode->replace_child ( domelement $newnode , domelement $oldnode )

(PHP 4.2) Esta función reemplaza el hijo nodo_antiguo con el nuevo nodo pasado. Si el nuevo nodo ya es un hijo, no será agregado una segunda vez. Si el nodo antiguo no puede encontrarse, la función devuelve FALSE. Si el reemplazo tiene éxito, se devuelve el nodo antiguo.

(PHP 4.3) Esta función reemplaza el hijo nodo_antiguo con el nodo_nuevo pasado, incluso si el nuevo nodo ya es un hijo del DomNode. Si nodo_nuevo ya había sido insertado en el documento, primero es desenlazado de su contexto actual. Si el nodo antiguo no puede ser encontrado, la función devuelve FALSE. Si el reemplazo tiene éxito, se devuelve el nodo antiguo. (Este comportamiento sigue las especificaciones del W3C).

Vea también domnode_append_child()



add a note add a note User Contributed Notes DomNode->replace_child
Beat 25-Nov-2010 10:32
This function behaves differently depending on PHP version:

- It forgets attributes of the new node in PHP 5.2,
- but copies them in PHP 5.3.

(at least when looking at the SimpleXMLElement mirror of the DOM being changed in DOM (by lack of equivalent methods in SimpleXMLElement).

Annoying different behavior / bug, but good to know. Would be good to update the doc about what the wanted behavor is

In both versions, children nodes are not coming together with the new node and need to be added one by one recursively. Just good to know too.
nospam at fivetide dot com 27-Feb-2004 02:25
There's a bug in the docs here. The order of arguments is the other way round i.e.
object DomNode->replace_child ( object newnode, object oldnode)

Here's an example of it working:

<?php
$doc
= domxml_new_doc("1.0");
$root = $doc->create_element("HTML");
$doc->append_child($root);
$head = $doc->create_element("HEAD");
$root->append_child($head);
$title = $doc->create_element("TITLE");
$head->append_child($title);
$text = $doc->create_text_node("This is the title");
$title->append_child($text);

echo
"<PRE>";
echo
htmlentities($doc->dump_mem(true));
echo
"</PRE>";

$newtitle = $doc->create_text_node("No, this is the title");
// object DomNode->replace_child ( object newnode, object oldnode)
$title->replace_child($newtitle,$title->first_child());

echo
"<PRE>";
echo
htmlentities($doc->dump_mem(true));
echo
"</PRE>";
?>

 
show source | credits | stats | sitemap | contact | advertising | mirror sites