phphtmlhtml-tablesimpledom

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


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


Solution

  • 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>';