Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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") . "
";
            }
        }
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
440 views
Welcome To Ask or Share your Answers For Others

1 Answer

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...