通过简单的dom解析器在html表中循环并获取php中的tr,th和td

时间:2019-01-10 17:54:48

标签: php html html-table simpledom

我需要使用简单的html dom解析器获取一个表,对其进行清理(删除attr和空格),然后再次将其输出。

我的问题是,我怎样才能遍历PHP并按顺序输出TH和TD? 目前它将TH作为TD处理,但我希望TH设置正确。

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

我尝试过foreach,但我认为我还需要其他东西。

忠于您的想法。

打招呼;

2 个答案:

答案 0 :(得分:1)

您将需要存储单元格的类型,将它们存储在行级别就足够了,因为它们都应该相同。然后,当您重建行时,使用此类型作为单元格类型来创建...

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

答案 1 :(得分:1)

您可以这样做:

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