【发布时间】:2011-07-12 04:45:46
【问题描述】:
在使用 Xpath 和 domDocument 获取与给定单词匹配的链接以显示时出现问题。在使用for($i=0;$i<$documentLinks->length;$i++){ 的位置似乎一切正常。
任何人都可以帮助我在这里出错的地方吗?
$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";
}
}
}
【问题讨论】:
-
根据php.net/manual/en/class.domnode.php,
$documentLinks不是字符串
标签: php domdocument xpath