Jaime Cross
Jaime Cross

Reputation: 523

Finding links matching given string in xpath/domdocument query

Having issues getting links that match a given word to display using Xpath and domDocument. Everything seems to work up to where for($i=0;$i<$documentLinks->length;$i++){ is used.

Can anyone help with where I am going wrong here?

$html  = '<ol>';
$html .= '  <li id="stuff-123"> some copy here </li>';
$html .= '  <li id="stuff-456"> some copy here <a href="http://domain.com">domain</a> </li>';
$html .= '  <li id="stuff-789"> some copy here </li>';
$html .= '</ol>';


    $dom = new DOMDocument();
    $dom->loadHTML($html);
    $xpath = new DOMXPath($dom); 
    $result = $xpath->query('//ol/li[starts-with(@id, "stuff")]');
    foreach($result as $e){
        $documentLinks = $e->getElementsByTagName('a')->item(0)->nodeValue;
        for($i=0;$i<$documentLinks->length;$i++){
            $documentLink = $documentLinks->item($i);
            if(preg_match("/domain/i", $documentLink->getAttribute("href"))){
              echo $documentLink->getAttribute("href") . "\n";
            }
        }
    }

Upvotes: 2

Views: 1568

Answers (2)

Gordon
Gordon

Reputation: 316969

You can fetch the href attribute directly via XPath

//ol/li[starts-with(@id, "stuff")]/a[contains(@href, "domain")]/@href

and then just do

foreach($result as $href){
    echo $href->nodeValue;
}

Note that contains function is case-sensitive though.

Upvotes: 1

Matt Wonlaw
Matt Wonlaw

Reputation: 12443

The line: $documentLinks = $e->getElementsByTagName('a')->item(0)->nodeValue;

should probably be: $documentLinks = $e->getElementsByTagName('a');


$e->getElementsByTagName('a')

returns all children of $e whose tag is <a ...> which means that

$e->getElementsByTagName('a')->item(0);

is returning the first link under $e

and $documentLinks = $e->getElementsByTagName('a')->item(0)->nodeValue; is returning the text of that first link.

http://php.net/manual/en/domdocument.getelementsbytagname.php

Upvotes: 1

Related Questions