DiDom\Document::find PHP Method

find() public method

Searches for an node in the DOM tree for a given XPath expression or a CSS selector.
public find ( string $expression, string $type = Query::TYPE_CSS, boolean $wrapElement = true ) : Element[] | DOMElement[]
$expression string XPath expression or a CSS selector
$type string The type of the expression
$wrapElement boolean Returns array of \DiDom\Element if true, otherwise array of \DOMElement
return Element[] | DOMElement[]
    public function find($expression, $type = Query::TYPE_CSS, $wrapElement = true)
    {
        $expression = Query::compile($expression, $type);
        $xpath = new DOMXPath($this->document);
        $xpath->registerNamespace("php", "http://php.net/xpath");
        $xpath->registerPhpFunctions();
        $nodeList = $xpath->query($expression);
        $result = [];
        if ($wrapElement) {
            foreach ($nodeList as $node) {
                $result[] = $this->wrapNode($node);
            }
        } else {
            foreach ($nodeList as $node) {
                $result[] = $node;
            }
        }
        return $result;
    }

Usage Example

Beispiel #1
0
 /**
  * @dataProvider findProvider
  */
 public function testFind($html, $selector, $type, $count)
 {
     $document = new Document($html, false);
     $elements = $document->find($selector, $type);
     $this->assertTrue(is_array($elements));
     $this->assertEquals($count, count($elements));
 }
All Usage Examples Of DiDom\Document::find