Goutte icon indicating copy to clipboard operation
Goutte copied to clipboard

count h1 tags

Open Takuaraa opened this issue 7 years ago • 3 comments

Hello,

Is there a way to count how many h1 tags a given url has? How can I increment $count using this: ?

$client = new Client();

    $crawler = $client->request('GET', $url);

    $count = 0;

    $crawler->filter('h1')->each(function ($node) {
      $count++;
      print $count++ . "<br>";
    });

This prints always 1 even if there are more h1 tags.

Thank you.

Takuaraa avatar Oct 25 '17 09:10 Takuaraa

If you just need the total count…

$h1 = $crawler->filter('h1')->each(function ($node) {
  return $node->text();
});

print count($h1);

If you want to print each increment…

$crawler->filter('h1')->each(function ($node) use ($count) {

Or…

$crawler->filter('h1')->each(function ($node) {
  global $count;

ssanders avatar Oct 25 '17 13:10 ssanders

Why not:

$crawler->filter('h1')->count();

dpde avatar Dec 12 '17 11:12 dpde

Why not:

$crawler->filter('h1')->count();

Best Solution

vinodsin avatar Sep 05 '19 11:09 vinodsin