1

我在 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 */
}

¿可以$crawler->filter接受各种条件并在一个foreach中完成吗?例如:

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

2 回答 2

2

filter 函数采用标准的 CSS 选择器,因此:

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

应该做的工作。

于 2015-07-09T08:31:25.633 回答
0

AFAIK 这是不可能的,但你可以这样做:

$crawler->filter('[rel="stylesheet"]')->addAll($crawler->filter('[type="text/css"]'));

这应该可以解决您的问题。

此外,您还可以使用 xpath 进行过滤。

于 2015-07-08T20:34:57.410 回答