2014-07-24 122 views
0

这里是我的代码:PHP - DOMXpath - 获取价值

$doc = new DOMDocument; 
    $doc->loadHTML($source); 

    $xpath = new DOMXPath($doc); 
    $result = $xpath->evaluate($xpath); 
foreach($result as $node) { 
    echo $node->nodeValue; 

    } 


//I'm trying to get the href attribute in: <a href="http://example.com/login">Log In</a> 

当我评价它,我只得到登录。

我想要得到的http://example.com/login

当我使用Python的LXML和评估的表达,它工作正常。

+0

你试过'$ node-> getAttribute('href');'? –

+0

它什么都不打印 – user3869692

+2

如果' - > nodeValue'给你“登录”,那么' - > getAttribute('href')'肯定会给你上面的URL。 –

回答

0
foreach($result as $node) 
{ 
echo $node->getAttribute('href'); 
} 

nodeValue属性用于获取节点的文本值。

getAttribute()方法返回属性的值。