PHP | DOMDocument getElementsByTagName() Function
Last Updated :
27 Aug, 2019
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
$xml = <<< XML
<?xml version= "1.0" encoding= "utf-8" ?>
<organization>
<name>GeeksforGeeks</name>
<address>Noida India</address>
<contact>
<email>abc@geeksforgeeks.org</email>
<mobile>+91-987654321</mobile>
</contact>
</organization>
XML;
$dom = new DOMDocument;
$dom ->loadXML( $xml );
$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
Share your thoughts in the comments
Please Login to comment...