使用Symfony CssSelector,如何忽略隐藏的元素

时间:2015-03-02 17:15:42

标签: php html symfony css-selectors

我正在使用Symphony CssSelectorSymphony DomCrawler

以下是我想要选择的几个例子。我遇到的问题是页面有时会在style="display:none"中抛出一个元素,有时它不会用span标记包装所有内容。我希望得到span class="moreselect"之间的所有内容,但只包含未隐藏的部分。

示例A:

<span class="selectme">
    <p>
        <span class="moreselect">
            This is
            <span>a string</span>
            <span style"display:none">hidden</span>
            <span>that isn't hidden</span>
        </span>
    </p>
</span>

例B:

<span class="selectme">
    <p>
        <span class="moreselect">
            This is
            <span>a string</span>
            that isn't hidden
        </span>
    </p>
</span>

这是我到目前为止所尝试过的。

$html->filter('.selectme .moreselect')->text();
// Example A Results: This is a string hidden that isn't hidden
// Example B Results: This is a string that isn't hidden

$html->filter('.selectme .moreselect span')->each(function($node) { echo $node->text(); });
// Example A Results: a string hidden that isn't hidden
// Example B Results: a string

无论哪种方式,我希望作为结果或组合的最终结果是:This is a string that isn't hidden

我看到有reduce()方法,但我尝试使用它的方法并不成功。我最终得到了\InvalidArgumentException

$html->filter('.selectme .moreselect')->reduce(function($node)
{
    if($node->attr('style') == 'display:none') {
        return false;
    } else {
        return true;
    }
})->each(function($node) { echo $node->text(); });

0 个答案:

没有答案