Friday, October 24, 2025
HomeLanguagesPHP | DOMNode replaceChild() Function

PHP | DOMNode replaceChild() Function

The DOMNode::replaceChild() function is an inbuilt function in PHP which is used replace the old child node with the passed new node. Further, if the new node is already a child it will not be added the second time. If the replacement succeeds the old node is returned.

Syntax:

DOMNode DOMNode::replaceChild( DOMNode $newnode, DOMNode $oldnode )

Parameters: This function accept two parameters as mentioned above and described below:

  • $newnode: It specifies the new node.
  • $oldnode: It specifies the old node.

Return Value: This function returns old node or FALSE if an error occur.

Exceptions: This function throws DOM_NO_MODIFICATION_ALLOWED_ERR, if this node is read-only or if the previous parent of the node being inserted is read-only, DOM_HIERARCHY_REQUEST_ERR, if this node is of a type that does not allow children of the type of the $newnode node, or if the node to put in is one of this node’s ancestors or this node itself, DOM_WRONG_DOCUMENT_ERR, if $newnode was created from a different document than the one that created this node, DOM_NOT_FOUND, if $oldnode is not a child of this node.

Below examples illustrate the DOMNode::replaceChild() function in PHP:

Program 1:




<?php
  
// Create a new DOMDocument instance
$document = new DOMDocument();
  
// Create a root element
$element = $document->appendChild(new DOMElement('root'));
  
// Create the text Node
$text1 = $document->createTextNode('Hello ! ');
$text2 = $document->createTextNode('Text to be replaced');
$text3 = $document->createTextNode('replaced');
  
// Append the nodes
$element->appendChild($text1);
$element->appendChild($text2);
  
// Replace the child
$element->replaceChild($text3, $text2);
  
// Render the output
echo $document->saveXML();


Output:

<?xml version="1.0"?>
<root>Hello ! replaced</root>

Example 2:




<?php
  
// Create a new DOMDocument instance
$document = new DOMDocument();
  
// Create a h1 element
$element = $document->appendChild(new DOMElement('h1'));
  
// Create the text Node
$text1 = $document->createTextNode('Geeksfor');
$text2 = $document->createTextNode('Text to be removed');
$text3 = $document->createTextNode('Geeks');
  
// Append the nodes
$element->appendChild($text1);
$element->appendChild($text2);
  
// Replace the child
$element->replaceChild($text3, $text2);
  
// Render the output
echo $document->saveXML();
?>


Output:

<?xml version="1.0"?>
<root>neveropen</root>

Reference: https://www.php.net/manual/en/domnode.replacechild.php

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS