Search code examples
phpsymfonydomcrawler

Symfony DomCrawler. Filter condition


I have this script in Symfony 2:

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\DomCrawler\Crawler;

class MyController extends Controller
{
....
foreach($crawler->filter('[type="text/css"]') as $content){
/* make things */
}
foreach($crawler->filter('[rel="stylesheet"]') as $content){
/* make things */
}

¿can $crawler->filter accept various conditions and do it in one foreach? For example:

foreach($crawler->filter('[rel="stylesheet"] OR [type="text/css"]') as $content){
/* make things */
}

Solution

  • The filter function takes a standard CSS selector, so:

    foreach ($crawler->filter('[rel="stylesheet"],[type="text/css"]') as $content) {
       /* make things */
    }
    

    Should do the job.