Saturday, September 28, 2024
Google search engine
HomeLanguagesPHP | DOMElement removeAttributeNode() Function

PHP | DOMElement removeAttributeNode() Function

The DOMElement::removeAttributeNode() function is an inbuilt function in PHP which is used to remove attribute from element.

Syntax:

bool DOMElement::removeAttributeNode( DOMAttr $oldnode )

Parameters: This function accepts a single parameter $oldnode which holds the attribute that to be removed.

Return Value: This function returns TRUE on success or FALSE on failure.

Exceptions: This function throws DOM_NO_MODIFICATION_ALLOWED_ERR, if the node is read-only and DOM_NOT_FOUND_ERROR if $oldnode is not a attribute of element.

Below given programs illustrate the DOMElement::removeAttributeNode() function in PHP:

Program 1:




<?php
  
// Create a new DOMDocument
$dom = new DOMDocument();
  
// Load the XML
$dom->loadXML("<?xml version=\"1.0\"?>
<root>
    <html>
        <h1 id=\"my_id\"> Geeksforneveropen </h1>
        <h2> Second heading </h2>
    </html>
</root>");
  
// Get the elements
$node = $dom->getElementsByTagName('h1')[0];
  
echo "Before the removal of attributes: <br>";
  
// Get the attribute name and value
$attribute = $node->attributes->item(0);
$attribute_name = $attribute->name;
$attribute_value = $attribute->value;
  
echo $attribute_name . ' => ';
echo $attribute_value;
  
// Get the attribute to remove
$oldnode = $node->getAttributeNode('id');
  
// Remove the id attribute
$node->removeAttributeNode($oldnode);
  
echo "<br>After the removal of attributes: <br>";
  
// Get the attribute name and value
$attribute = $node->attributes->item(0);
$attribute_name = $attribute->name . ' => ';
$attribute_value = $attribute->value;
echo $attribute_name;
echo $attribute_value;
?>


Output:

Before the removal of attributes:
id => my_id
After the removal of attributes:
=>          // Empty string means attribute is removed

Program 2:




<?php
  
// Create a new DOMDocument
$dom = new DOMDocument();
  
// Load the XML
$dom->loadXML("<?xml version=\"1.0\"?>
<root>
    <html>
        <h1 id=\"my_id\" style=\"color:green;\" 
          class=\"my_class\"> Geeksforneveropen </h1>
        <h2> Second heading </h2>
    </html>
</root>");
  
// Get the elements
$node = $dom->getElementsByTagName('h1')[0];
  
echo "Before the removal of attributes: <br>";
  
// Get the attribute to remove
$oldnode = $node->getAttributeNode('id');
  
// Get the attribute count
$attributeCount = $node->attributes->count();
echo 'No of attributes => ' . $attributeCount;
  
// Remove the id attribute
$node->removeAttributeNode($oldnode);
  
echo "<br>After the removal of attributes: <br>";
  
// Get the attribute count
$attributeCount = $node->attributes->count();
echo 'No of attributes => ' . $attributeCount;
?>


Output:

Before the removal of attributes:
No of attributes => 3
After the removal of attributes:
No of attributes => 2

Reference: https://www.php.net/manual/en/domelement.removeattributenode.php

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments