Finding links matching given string in xpath/domdocument query

前端 未结 2 1810
野性不改
野性不改 2020-12-18 14:17

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->leng

相关标签:
2条回答
  • 2020-12-18 14:28

    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.

    0 讨论(0)
  • 2020-12-18 14:32

    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

    0 讨论(0)
提交回复
热议问题