Thursday, October 9, 2025
HomeLanguagesPHP | DOMDocument getElementsByTagName() Function

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 parameter $name which holds the local tag name to match. The value * is used to match all tags.

Return Value: This function returns a new DOMNodeList object containing all the matched elements.

Below program illustrates the DOMDocument::getElementsByTagName() function in PHP:

Program:




<?php
  
// Store the XML document to the variable
$xml = <<< XML
<?xml version="1.0" encoding="utf-8"?>
<organization>
    <name>neveropen</name>
    <address>Noida India</address>
    <contact>
        <email>abc@geeksforgeeks.org</email>
        <mobile>+91-987654321</mobile>
    </contact>
</organization>
XML;
  
// Create new DOMDocument
$dom = new DOMDocument;
  
// Load the XML document
$dom->loadXML($xml);
  
// Use getElementsByTagName() function to search
// all elements with given local tag name
$org = $dom->getElementsByTagName('contact');
  
foreach ($org as $contact) {
    echo $contact->nodeValue, PHP_EOL;
}
?>


Output:

abc@geeksforgeeks.org
+91-987654321

Reference: https://www.php.net/manual/en/domdocument.getelementsbytagname.php

RELATED ARTICLES

Most Popular

Dominic
32346 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6714 POSTS0 COMMENTS
Nicole Veronica
11877 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11940 POSTS0 COMMENTS
Shaida Kate Naidoo
6835 POSTS0 COMMENTS
Ted Musemwa
7094 POSTS0 COMMENTS
Thapelo Manthata
6789 POSTS0 COMMENTS
Umr Jansen
6791 POSTS0 COMMENTS