I am doing a simple web scraping for images.
$images = $dom->getElementsByTagName('img');
$images return
DOMNodeList Object
(
[length] => 19
)
If I print $src inside foreach loop, it shows 19 results.From those 19 results if i again print $src after http matching it shows 11 results. But I want first 5 result from those 11 results after preg_match.
How is it possible?
foreach ($images as $keys=>$image) {
$src = $image->getAttribute('src');
if(preg_match('/^http/', $src)){
}
}
Test with the below code
$loopCount = 1;
foreach ($images as $keys=>$image) {
$src = $image->getAttribute('src');
if(preg_match('/^http/', $src)) {
//assuming here you need to check count
$loopCount ++;
//your action
if($loopCount > 5) {
break; //to avoid unnecessary loops
}
}
}
It will give you the first 5 regex matching records