PHP | DOMElement getElementsByTagName() Function Last Updated : 28 Apr, 2025 Comments Improve Suggest changes Like Article Like Report 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 all the tags. Return Value: This function returns a DOMNodeList value containing all descendant elements with a given tag name, in the order in which they are encountered in a preorder traversal of this element tree. Below examples illustrate the DOMElement::getElementsByTagName() function in PHP: Example 1: PHP <?php // Create a new DOMDocument $dom = new DOMDocument(); // Load the XML $dom->loadXML("<?xml version=\"1.0\"?> <root> <body> <div> <h1 style=\"color:red;\"> Hello, this is my red heading. </h1> <h1 style=\"color:green;\"> Hello, this is my green heading. </h1> <h1 style=\"color:blue;\"> Hello, this is my blue heading. </h1> </div> </body> </root>"); // Save the XML $nodeList = $dom->getElementsByTagName('h1'); foreach ($nodeList as $node) { // Get the attribute value of style echo $node->getAttribute('style') . '<br>'; } ?> Output: color:red; color:green; color:blue; Example 2: PHP <?php // Create a new DOMDocument $dom = new DOMDocument(); // Load the XML $dom->loadXML("<?xml version=\"1.0\"?> <root> <body> <div> <p> Hello, this is my first paragraph. </p> <p> Hello, this is my second paragraph. </p> <p> Hello, this is my third paragraph. </p> </div> </body> </root>"); // Get the element by tagname $nodeList = $dom->getElementsByTagName('p'); foreach ($nodeList as $node) { // Get the textContent echo $node->textContent . '<br>'; } ?> Output: Hello, this is my first paragraph. Hello, this is my second paragraph. Hello, this is my third paragraph. Reference: https://www.php.net/manual/en/domelement.getelementsbytagname.php Comment More infoAdvertise with us Next Article PHP | DOMElement getElementsByTagName() Function G gurrrung Follow Improve Article Tags : Web Technologies PHP PHP-function PHP-DOM Similar Reads 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 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 | DOMDocument getElementById() Function 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. Retu 2 min read PHP | DOMElement getAttribute() Function The DOMElement::getAttribute() function is an inbuilt function in PHP which is used to get the value of the attribute with name for the current node. Syntax: string DOMElement::getAttribute( string $name ) Parameters: This function accepts a single parameter $name which holds the name of the attribu 1 min read Like