在DOMXPath中选择“类名”值

时间:2012-12-18 02:42:02

标签: php html dom domdocument domxpath

  

可能重复:
  How to extract a node attribute from XML using PHP’s DOM Parser

我有这样的HTML:

   <tr class="calendar_row" data-eventid="48256">
   ...
   </tr>

我只想在网页上选择值或data-eventid,但我不知道如何在xpath中执行此操作。有可能吗?

1 个答案:

答案 0 :(得分:2)

您可以使用@来获取属性。

//assume $dom is a DOMDocument

$finder = new DOMXPath($dom);
//  //tr[@data-eventid] = all tr nodes that have a data-eventid attribute
//    /@data-eventid = the attribute node itself as opposed to the tr node
$nodes = $finder->query('//tr[@data-eventid]/@data-eventid');

foreach($nodes as $node) {
    echo $node->nodeValue."\n"; // echos your data-eventid value ie. 48256
}