문제

I'm using Simple HTML DOM to get a certain element like this:

foreach($html->find('img') as $d) { 
 echo $d->outertext;
}

This will echo all the images. Let's say for example I only need image with index (meaning relative to all images) number 3,7,14 and > 15. Is there a way to do something this complicated?

도움이 되었습니까?

해결책

find returns an array so just use the index

$imgs =$html->find('img');

$imgs[3];
$imgs[7];
$imgs[14];

for($i=15;$i<count($imgs);$i++){
  $imgs[$i];
}

다른 팁

Probably the simplest way is to add all the img tags to an array, and from there you can extract them according to index number.

You can accomplish this with a $count variable and in_array(). Declare the count variable before the loop, and declare the array of the required IDs. And in the loop, you can use an if statement to check if the image ID is in the array or greater than 15.

$count = 1;
$ids = array(3, 7, 14);

foreach($html->find('img') as $d) { 
if(in_array($count, $ids) || $count > 15){

 echo $d->outertext;
 $count++;    

}

Hope this helps!

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top