Skip to content
Related Articles
Open in App
Not now

Related Articles

PHP | DOMDocument getElementsByTagName() Function

Improve Article
Save Article
  • Last Updated : 27 Aug, 2019
Improve Article
Save Article

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.


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:


// Store the XML document to the variable
$xml = <<< XML
<?xml version="1.0" encoding="utf-8"?>
    <address>Noida India</address>
// Create new DOMDocument
$dom = new DOMDocument;
// Load the XML document
// 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;



My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!