The simple answer is to use xpath:
$dom = new DomDocument();
$dom->loadHtml($html);
$xpath = new DomXpath($dom);
$div = $xpath->query('//*[@class="foo"]')->item(0);
But it will not accept spaces. Therefore, to select a class, separated by a space, use this query:
//*[contains(concat(' ', normalize-space(@class), ' '), ' class ')
source
share