Goutte
Goutte copied to clipboard
count h1 tags
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.
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;
Why not:
$crawler->filter('h1')->count();
Why not:
$crawler->filter('h1')->count();
Best Solution