$doc = new DomDocument(); $doc->loadHTML($htmlString); $anchorTags = $doc->getElementsByTagName('a'); foreach ($anchorTags as $anchor) { echo $anchor->getAttribute('href'); }
tags in an XML document
$doc = new DomDocument(); $doc->loadXML($xmlString); $paragraphTags = $doc->getElementsByTagName('p'); foreach ($paragraphTags as $paragraph) { echo $paragraph->nodeValue; }This example is similar to the previous one, except it searches for
tags in an XML document instead of an HTML document. The code then loops through each paragraph tag and prints out its node value. In both examples, the PHP DomDocument package/library is being used.