Skip to content
Related Articles

Related Articles

Improve Article
PHP | DOMNode removeChild() Function
  • Last Updated : 02 Mar, 2020

The DOMNode::removeChild() function is an inbuilt function in PHP which is used remove child from list of children.

Syntax:

DOMNode DOMNode::removeChild( DOMNode $oldnode )

Parameters: This function accepts a single parameter $oldnode which holds the child to remove.

Return Value: This function returns the removed child on success.

Exceptions: This function throws DOM_NO_MODIFICATION_ALLOWED_ERR, if the node is readonly and DOM_NOT_FOUND, if $oldnode is not a child of this node.



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

Example 1:




<?php
  
// Create a new DOMDocument instance
$document = new DOMDocument();
  
// Create a div element
$element = $document-> 
        appendChild(new DOMElement('div'));
  
// Create a text Node
$text1 = $document->
          createTextNode('GeeksforGeeks');
  
// Append the nodes
$element->appendChild($text1);
  
// Remove the child
$element->removeChild($text1);
  
// Render the XML
echo $document->saveXML();
?>

Output: Press Ctrl+U to see the XML

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('GeeksforGeeks');
$text2 = $document->
          createTextNode('Text to be removed');
  
// Append the nodes
$element->appendChild($text1);
$element->appendChild($text2);
  
// Remove the child
$element->removeChild($text2);
  
// Render the output
echo $document->saveXML();
?>

Output:

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




My Personal Notes arrow_drop_up
Recommended Articles
Page :