Skip to content
Advertisement

simple-html-dom get table id by class name

I’am trying to build a scraper using simple-html-dom.

Each tr has the class “event-listing” as well as a unique id. Everything is working but I cannot figure out how to get and store the id’s.

Here’s my code:

foreach($html->find('tr[class=event-listing]') as $event) 
     {
       $item['date'] = trim($event->find('td', 0)->plaintext); 
       $first_trim = ltrim($event->find('td', 1)->plaintext, 'at ,vs.');
       $item['opponent'] = rtrim($first_trim, ' * '); 
       $item['location'] = trim($event->find('td', 2)->plaintext); 
       $item['time'] = trim($event->find('td', 3)->plaintext);
       $sched[] = $item;
     }

Advertisement

Answer

You don’t need anything special $event->id would work just fine

$string = '<tr valign="TOP" bgcolor="#d1d1d1" class="event-listing" title="2012,4,18,21,00,00" id="1444896"> <td class="row-text">&nbsp;</td> <td class="row-text">vs. Northern Colorado *</td> <td class="row-text">Orem, Utah</td> <td class="row-text">W, 11-6</td> </tr>';
$html = str_get_html($string);

$sched = array();
foreach ( $html->find('tr[class=event-listing]') as $event ) {
    $item['date'] = trim($event->find('td', 0)->plaintext);
    $first_trim = ltrim($event->find('td', 1)->plaintext, 'at ,vs.');
    $item['opponent'] = rtrim($first_trim, ' * ');
    $item['location'] = trim($event->find('td', 2)->plaintext);
    $item['time'] = trim($event->find('td', 3)->plaintext);
    $item['id'] = $event->id;
    $sched[] = $item;
}

var_dump($sched);

Output

array
  0 => 
    array
      'date' => string '&nbsp;' (length=6)
      'opponent' => string 'Northern Colorado' (length=17)
      'location' => string 'Orem, Utah' (length=10)
      'time' => string 'W, 11-6' (length=7)
      'id' => string '1444896' (length=7)
                         ^------------------------------ ID Captured 
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement