PHP | DOMDocument getElementById() Function Last Updated : 20 Feb, 2020 Comments Improve Suggest changes Like Article Like Report The DOMDocument::getElementById() function is an inbuilt function in PHP which is used to search for an element with a certain id. Syntax: DOMElement DOMDocument::getElementById( string $elementId ) Parameters:This function accepts a single parameter $elementId which holds the id to search for. Return Value: This function returns the DOMElement or NULL if the element is not found. Below given programs illustrate the DOMDocument::getElementById() function in PHP: Program 1: In this program we will get the tagname of element with certain id. php <?php // Create a new DOM Document $dom = new DOMDocument('1.0', 'iso-8859-1'); // Enable validate on parse $dom->validateOnParse = true; // Create a div element $element = $dom->appendChild(new DOMElement('div')); // Create a id attribute to div $attr = $element->setAttributeNode( new DOMAttr('id', 'my_id')); // Set that attribute as id $element->setIDAttribute('id', true); // Get the tag name $tagname = $dom->getElementById('my_id')->tagName; echo $tagname; ?> Output: div // Because id 'my_id' is applied to div tag. Program 2: In this program we will get the content of element with certain id. php <?php // Create a new DOM Document $dom = new DOMDocument('1.0', 'iso-8859-1'); // Enable validate on parse $dom->validateOnParse = true; // Create a div element $element = $dom->appendChild(new DOMElement('div', 'Hey, this is the text content of the div element.')); // Create a id attribute to div $attr = $element->setAttributeNode( new DOMAttr('id', 'my_id')); // Set that attribute as id $element->setIDAttribute('id', true); // Get the tag content $tagcontent = $dom->getElementById('my_id')->textContent; echo $tagcontent; ?> Output: Hey, this is the text content of the div element. Reference: https://www.php.net/manual/en/domdocument.getelementbyid.php Comment More infoAdvertise with us Next Article PHP | DOMDocument getElementById() Function G gurrrung Follow Improve Article Tags : Web Technologies PHP PHP-function PHP-DOM Similar Reads PHP | DOMDocument getElementsByTagName() Function The DOMDocument::getElementsByTagName() function is an inbuilt function in PHP which is used to return a new instance of class DOMNodeList which contains all the elements of local tag name. Syntax: DOMNodeList DOMDocument::getElementsByTagName( string $name ) Parameters: This function accepts single 1 min read PHP | DOMDocument getElementsByTagnameNS() Function The DOMDocument::getElementsByTagNameNS() function is an inbuilt function in PHP which is used to search for all elements with given tag name in specified namespace. Syntax: DOMNodeList DOMDocument::getElementsByTagNameNS( string $namespaceURI, string $localName ) Parameters: This function accepts t 2 min read PHP | DOMElement getElementsByTagName() Function The DOMElement::getElementsByTagName() function is an inbuilt function in PHP which is used to get the elements by tagname. Syntax:Â DOMNodeList DOMElement::getElementsByTagName( string $name ) Parameters: This function accepts a single parameter $name which holds the tag name or use * for getting a 2 min read PHP | DOMDocument load() Function The DOMDocument::load() function is an inbuilt function in PHP which is used to load an XML document from a file. Syntax: mixed DOMDocument::load( string $filename, int $options = 0 ) Parameters: This function accepts two parameters as mentioned above and described below: $filename: This parameter h 1 min read PHP | DOMElement getElementsByTagNameNS() Function The DOMElement::getElementsByTagNameNS() function is an inbuilt function in PHP which is used to get all the descendant elements with a given localName and namespaceURI. Syntax: DOMNodeList DOMElement::getElementsByTagNameNS( string $namespaceURI, string $localName ) Parameters: This function accept 2 min read Like