PHP | DOMElement removeAttribute() Function - GeeksforGeeks

26 Feb, 2020
The DOMElement::removeAttribute() function is an inbuilt function in PHP which is used to remove a attribute with specific name from the element.
Syntax:
bool DOMElement::removeAttribute( string $name )
Parameters: This function accepts a single parameter $name which holds the name of the attribute.
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.
Below examples illustrate the DOMElement::removeAttribute() function in PHP:
Example 1:
<?php  // Create a new DOMDocument$dom = new DOMDocument();  // Load the XML$dom->loadXML("<?xml version=\"1.0\"?><root>    <html>        <h1 id=\"my_id\"> Geeksforgeeks </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;  // Remove the id attribute$node->removeAttribute('id');  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 value means attribute is removed
Example 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\">             Geeksforgeeks         </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 count$attributeCount = $node->attributes->count();echo 'No of attributes => ' . $attributeCount;   // Remove the id attribute$node->removeAttribute('id');   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.removeattribute.php
PHP-DOM
PHP-function
PHP
Web Technologies
PHP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Insert Form Data into Database using PHP ?
How to convert array to string in PHP ?
How to Upload Image into Database and Display it using PHP ?
How to check whether an array is empty using PHP?
PHP | Converting string to Date and DateTime
Remove elements from a JavaScript Array
Installation of Node.js on Linux
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?