Search code examples
phphtml-tablehtml-parsingsimple-html-domskip

How to skip last n rows with simple_html_dom or Dom Document?


Is there a way to skip always the last n rows of a parsed table via simple_html_dom or dom document?

I tried to work with fixed rownumbers, but since the source file could change its amount of rows, it didn't work out.

This is my standard code for parsing a table. Do you have an idea or hint for me, how to skip always last two rows?

$table = $html->find('table', 1);
$rowData = array();

    foreach($table->find('tr') as $row) {
        // initialize array to store the cell data from each row

    $roster = array();
        foreach($row->find('td') as $cell) {
        $roster[] = $cell->innertext;
    }
    foreach($row->find('th') as $cell) {
        $roster[] = $cell->innertext;
    }
        $rowData[] = $roster;
    }

        foreach ($rowData as $row => $tr) {
            echo '<tr>';
            foreach ($tr as $td)
            echo '<td>' . $td .'</td>';
            echo '</tr>';
        }
        echo '</table></td><td>';

Solution

  • You can simply pop two items from your array of find results:

    $rows = $table->find('tr');
    array_pop($rows);
    array_pop($rows);
    
    foreach ($rows as $row) {
        // do stuff here
    }
    

    Sure, it's not an ideal solution, as an alternative you can get count of found rows and use index to control current element in foreach:

    $rows = $table->find('tr');
    $limit = count($rows) - 2;
    $counter = 0;
    
    foreach ($rows as $row) {
        if ($counter++ < $limit) {
            break;
        }
    
        // do stuff
    }