0

Is it possible to get only an n number of items using dom crawler ?

I have

  `$items = $website->filter('ul.listnews li'); 

   $items>each(function($node,$con){

 }`

But I want to get only the first 5 items from the list. I tried running a for loop but I couldn't get it to work. Any ideas on how I could do it ?

Bazinga777
  • 5,140
  • 13
  • 53
  • 92

1 Answers1

1

You can use reduce method in chain:

$items = $website
         ->filter('ul.listnews li')
         ->reduce(function (Crawler $node, $i) {
             return $i < 5;
          }); 
Bogdan Kuštan
  • 5,427
  • 1
  • 21
  • 30