loop through html table and get tr, th and td in php with simple dom parser

3.1k views Asked by At

I need to get a table with simple html dom parser, clean it (remove attr and spaces), then output it again.

My question is, how can i loop through with PHP and output the TH and TD in one sequence? At the moment it will handle the TH as a TD but i like to have the TH set correctly.

<table>
    <tbody>
        <tr>
            <th>Date1</th>
            <th>Date2</th>
        </tr>
        <tr>
            <td>01.01.2019</td>
            <td>05.01.2019</td>
        </tr>
    </tbody>
</table>

require('simple_html_dom.php');
$html = file_get_html( "template.html" );
$table = $html->find('table', 0);
$rowData = array();

foreach($table->find('tr') as $row) {
    $keeper = array();

    foreach($row->find('td, th') as $cell) {
        $keeper[] = $cell->plaintext;
    }
    $rowData[] = $keeper;
}

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

I tried something with the foreach, but i think i need something else.

ty for your ideas.

greet;s

2

There are 2 answers

1
Saleemi On BEST ANSWER

You can do it like this:

require('simple_html_dom.php');
$html = file_get_html( "template.html" );
$table = $html->find('table', 0);
$rowData = array();

foreach($table->find('tr') as $row) {
    $keeper = array();

    foreach($row->find('td, th') as $cell) {
        $data = array();
        $data['tag'] = $cell->tag;                      //stored Tag and Plain Text
        $data['plaintext'] = $cell->plaintext;
        $keeper[] = $data;
    }
    $rowData[] = $keeper;
}

echo '<table>';
foreach ($rowData as $row => $tr) {
    echo '<tr>'; 
    foreach ($tr as $td)
        echo '<'.$td['tag'].'>' . $td['plaintext'] .'</'.$td['tag'].'>';  // Tag used
    echo '</tr>';
}
echo '</table>';
0
Nigel Ren On

You would need to store the type of cells, it would be enough to store them at row level as they should all be the same. Then when you rebuild the rows, use this type as the cell type to create...

foreach($table->find('tr') as $row) {
    $keeper = array();

    foreach($row->find('td, th') as $cell) {
        $keeper[] = $cell->plaintext;
    }
    // Type is the 2nd & 3rd chars of html - <th>content</th> gives th
    // Store type and cell data as two elements of the rowData
    $rowData[] = ["type" => substr($cell,1,2), "cells" => $keeper];
}

echo '<table>';
foreach ($rowData as $row => $tr) {
    echo '<tr>';
    // Loop over the cells of the row
    foreach ($tr["cells"] as $td)
        // Output row type as the element type
        echo "<$tr[type]>" . $td ."</$tr[type]>";
        echo '</tr>';
}
echo '</table>';