4

How can I get all the rows with a specific class name such as:

<tr class="dailyeventtext" bgcolor="#cfcfcf" valign="top">

and then place each cell in that row into an array?

I used cURL to get the page off the client's server.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Anderson
  • 101
  • 4
  • 12
  • Can you define *place each cell in that row into an array* better? DOM nodes? Text content? Inner HTML? – alex Sep 26 '11 at 02:21

1 Answers1

7
$matches = array();

$dom = new DOMDocument;

$dom->loadHTML($html);

foreach($dom->getElementsByTagName('tr') as $tr) {
    if ( ! $tr->hasAttribute('class')) {
       continue;
    }

    $class = explode(' ', $tr->getAttribute('class'));

    if (in_array('dailyeventtext', $class)) {
       $matches[] = $tr->getElementsByTagName('td');
    }

}
alex
  • 479,566
  • 201
  • 878
  • 984
  • Thank you Alex. Now how do I get the values of whats in each cell? Thank you. – Anderson Sep 26 '11 at 02:38
  • @Anderson: Do you mean the text of each cell as separate array elements? Just `array_map()` the cols, returning `textContent` property. – alex Sep 26 '11 at 02:39