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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…