PHPHtmlParser\Selector::find PHP Method

find() public method

Attempts to find the selectors starting from the given node object.
public find ( AbstractNode $node ) : Collection
$node PHPHtmlParser\Dom\AbstractNode
return PHPHtmlParser\Dom\Collection
    public function find(AbstractNode $node)
    {
        $results = new Collection();
        foreach ($this->selectors as $selector) {
            $nodes = [$node];
            if (count($selector) == 0) {
                continue;
            }
            $options = [];
            foreach ($selector as $rule) {
                if ($rule['alterNext']) {
                    $options[] = $this->alterNext($rule);
                    continue;
                }
                $nodes = $this->seek($nodes, $rule, $options);
                // clear the options
                $options = [];
            }
            // this is the final set of nodes
            foreach ($nodes as $result) {
                $results[] = $result;
            }
        }
        return $results;
    }

Usage Example

 public function testToArray()
 {
     $root = new HtmlNode(new Tag('root'));
     $parent = new HtmlNode(new Tag('div'));
     $child1 = new HtmlNode(new Tag('a'));
     $child2 = new HtmlNode(new Tag('p'));
     $child3 = new HtmlNode(new Tag('a'));
     $root->addChild($parent);
     $parent->addChild($child1);
     $parent->addChild($child2);
     $child2->addChild($child3);
     $selector = new Selector('a');
     $collection = $selector->find($root);
     $array = $collection->toArray();
     $lastA = end($array);
     $this->assertEquals($child3->id(), $lastA->id());
 }
All Usage Examples Of PHPHtmlParser\Selector::find