Umair Umair - 1 year ago 144
PHP Question

Use FILTER() inside Foreach loop - PHP Goutte

I have this code

require_once 'vendor/autoload.php';

use Symfony\Component\DomCrawler\Crawler;

$html = <<<'HTML'
<!DOCTYPE html>
<p class="message">Hello World!
<span>This is span</span>
<p>Hello Crawler!
<span>This is span</span>

$crawler = new Crawler($html);

foreach ($crawler->filter('body') as $domElement) {

I am getting this error .

Fatal error: Call to undefined method DOMElement::filter()

I need to know how do I select elements using
CSS Selectors
inside that Foreach loop?

I searched a lot but did not find anything.

Answer Source

You can just use this.

$crawler = new Crawler($html);

$crawler->filter('body')->each(function(Crawler $node, $i){
    return $node->filter('span');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download