【问题标题】:Symfony DomCrawler. Filter conditionSymfony DomCrawler。过滤条件
【发布时间】:2015-07-08 20:05:39
【问题描述】:

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

【问题讨论】:

    标签: php symfony domcrawler


    【解决方案1】:

    过滤器函数采用标准的 CSS 选择器,所以:

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

    应该做的工作。

    【讨论】:

      【解决方案2】:

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

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

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-11
        相关资源
        最近更新 更多