-1

Possible Duplicate:
How to extract a node attribute from XML using PHP’s DOM Parser

I have HTML like this:

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

I just want to select the value or data-eventid across the web page but I don't have any idea how to do it in xpath. Is it possible?

Community
  • 1
  • 1
Vainglory07
  • 5,073
  • 10
  • 43
  • 77

1 Answers1

2

You can use @ to get at attributes.

//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
}
prodigitalson
  • 60,050
  • 10
  • 100
  • 114