$doc = new DOMDocument(); $doc->loadHTML($html); $xpath = new DOMXPath($doc); $titles = $xpath->evaluate('/html/head/title'); foreach ($titles as $title) { echo $title->nodeValue; }
$doc = new DOMDocument(); $doc->loadHTML($html); $xpath = new DOMXPath($doc); $imgs = $xpath->evaluate('//div/img[@alt="specific_text"]'); foreach ($imgs as $img) { echo $img->getAttribute('src'); }
$doc = new DOMDocument(); $doc->loadHTML($html); $xpath = new DOMXPath($doc); $a = $xpath->evaluate('//ul[@class="my-class"]/li[1]/a[@class="link-class"]/@href'); echo $a->item(0)->nodeValue;The `DOMXPath` class is part of the PHP core, so there is no need to install any external library or package to use it.