使用简单的dom解析器在php中循环遍历html表并获取tr、th和td

使用简单的dom解析器在php中循环遍历html表并获取tr、th和td,php,html,html-table,simpledom,Php,Html,Html Table,Simpledom,我需要用简单的HTMLDOM解析器获取一个表,清理它(删除attr和空格),然后再次输出它 我的问题是,如何使用PHP循环并以一个序列输出TH和TD? 目前它将把TH作为TD处理,但我希望TH设置正确 日期1 日期2 01.01.2019 05.01.2019 您需要存储单元格的类型,将它们存储在行级别就足够了,因为它们应该是相同的。然后在重建行时,使用此类型作为要创建的单元格类型 foreach($table->find('tr') as $row) { $keeper =

我需要用简单的HTMLDOM解析器获取一个表,清理它(删除attr和空格),然后再次输出它

我的问题是,如何使用PHP循环并以一个序列输出TH和TD? 目前它将把TH作为TD处理,但我希望TH设置正确


日期1
日期2
01.01.2019
05.01.2019

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

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>';
foreach($table->find('tr')作为$row){
$keeper=array();
foreach($row->find('td,th')作为$cell){
$keeper[]=$cell->plaintext;
}
//类型是html的第2和第3个字符-内容为
//将类型和单元格数据存储为行数据的两个元素
$rowData[]=[“type”=>substr($cell,1,2),“cells”=>$keeper];
}
回声';
foreach($rowdataas$row=>$tr){
回声';
//在行的单元格上循环
foreach($tr[“单元格”]作为$td)
//将行类型输出为元素类型
回声“$td.”;
回声';
}
回声';

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

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>';
foreach($table->find('tr')作为$row){
$keeper=array();
foreach($row->find('td,th')作为$cell){
$keeper[]=$cell->plaintext;
}
//类型是html的第2和第3个字符-内容为
//将类型和单元格数据存储为行数据的两个元素
$rowData[]=[“type”=>substr($cell,1,2),“cells”=>$keeper];
}
回声';
foreach($rowdataas$row=>$tr){
回声';
//在行的单元格上循环
foreach($tr[“单元格”]作为$td)
//将行类型输出为元素类型
回声“$td.”;
回声';
}
回声';

您可以这样做:

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>';
require('simple_html_dom.php');
$html=file_get_html(“template.html”);
$table=$html->find('table',0);
$rowData=array();
foreach($table->find('tr')作为$row){
$keeper=array();
foreach($row->find('td,th')作为$cell){
$data=array();
$data['tag']=$cell->tag;//存储的标记和纯文本
$data['plaintext']=$cell->plaintext;
$keeper[]=$data;
}
$rowData[]=$keeper;
}
回声';
foreach($rowdataas$row=>$tr){
回声';
foreach($tr as$td)
回显“”。$td['plaintext'].';//使用了标记
回声';
}
回声';

您可以这样做:

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>';
require('simple_html_dom.php');
$html=file_get_html(“template.html”);
$table=$html->find('table',0);
$rowData=array();
foreach($table->find('tr')作为$row){
$keeper=array();
foreach($row->find('td,th')作为$cell){
$data=array();
$data['tag']=$cell->tag;//存储的标记和纯文本
$data['plaintext']=$cell->plaintext;
$keeper[]=$data;
}
$rowData[]=$keeper;
}
回声';
foreach($rowdataas$row=>$tr){
回声';
foreach($tr as$td)
回显“”。$td['plaintext'].';//使用了标记
回声';
}
回声';

只是使用?我不理解这个问题-“我用foreach尝试了一些东西,但我认为我需要其他东西。”您尝试了
foreach
,但显然不起作用,您需要其他东西。什么不起作用?还有什么?只是使用?我不明白这个问题——“我用foreach试过一些东西,但我想我需要其他东西。”你试过
foreach
,但显然不起作用,你需要其他东西。什么不起作用?还有什么?谢谢你的投资,这真是太好了!谢谢你的投资,这真是太好了!