【发布时间】:2011-09-05 22:36:12
【问题描述】:
当我使用以下代码使用var_dump 时,我能够使用xpath 提取必要的信息。当我尝试添加一个 foreach 循环以返回所有 ["href"] 值时,我得到一个空白页,有什么想法我搞砸了吗?
$dom = new DOMDocument();
@$dom->loadHTML($source);
$xml = simplexml_import_dom($dom);
$rss = $xml->xpath("/html/body//a[@class='highzoom1']");
$links = $rss->href;
foreach ($links as $link){
echo $link;
}
这是信息数组。
array(96) {
[0]=>
object(SimpleXMLElement)#3 (2) {
["@attributes"]=>
array(2) {
["href"]=>
string(49) "/p/18351/test1.html"
["class"]=>
string(10) "highzoom1"
}
[0]=>
string(36) ""test1"
}
[1]=>
object(SimpleXMLElement)#4 (2) {
["@attributes"]=>
array(2) {
["href"]=>
string(43) "/p/18351/test2.html"
["class"]=>
string(10) "highzoom1"
}
[0]=>
string(30) ""test2"
}
[2]=>
object(SimpleXMLElement)#5 (2) {
["@attributes"]=>
array(2) {
["href"]=>
string(48) "/p/18351/test3.html"
["class"]=>
string(10) "highzoom1"
}
[0]=>
string(35) ""test3"
}
【问题讨论】:
-
能否请您在原始XML的内容中发布以供参考?
-
好问题,+1。请参阅我的答案以获得简短的单行 XPath 解决方案:)
标签: php xpath foreach simplexml