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 that won't accept spaces. So to select by space separated class, use this query:
//*[contains(concat(' ', normalize-space(@class), ' '), ' class ')
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…