Tuesday, November 19, 2024
Google search engine
HomeLanguagesPHP | DOMNode removeChild() Function

PHP | DOMNode removeChild() Function

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('neveropen');
  
// 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('neveropen');
$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

RELATED ARTICLES

Most Popular

Recent Comments