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