With XPath you can get all the images as follows:
$xpath = new DOMXPath($dom); $list = $xpath->query('//img');
Then you limit the results to only repetition for the first five.
for ($i = 0, $n = min(5, $list->length); $i < $n; ++$i) { $node = $list->item(0); }
XPath is very versatile thanks to the expression language . However, in this particular case, you may not need all this power, and a simple $list = $dom->getElementsByTagName('img') will give the same set of results.
source share