<?php
$document = new DOMDocument();
$html = <<<END
<html>
<div class="foo">
<span id="hello">Hello!</span>
</div>
<div>
<div class="foo"></div>
</div>
</html>
END;
$document->loadHTML($html);
$xpath = new DOMXPath($document);
$results = $xpath->query("//*[@class='foo']");
foreach($results as $node) {
var_dump($node);
}
?>