假设我有以下HTML表格:
<table>
<tbody>
<tr>
<th>Column 1</th>
<th>Column 2</th>
<th>Column 3</th>
<th>Column 4</th>
<th>Column 5</th>
</tr>
<tr>
<td>Column 1</td>
<td>Column 2</td>
<td>Column 3</td>
<td>Column 4</td>
<td>Column 5</td>
</tr>
</tbody>
</table>
如何使用简单的HTML DOM解析器返回没有第4列和第5列的表?表的前三列?我从这开始就得到每一行:
foreach ($html->find('tr') as $row) {
}
我会运行另一个for循环迭代细胞吗?
答案 0 :(得分:1)
您需要删除每行的第4列和第5列(如果它们存在,我只是假设它们存在)。请注意元素的子元素为零索引:
foreach ($html->find('tr') as $row) {
// remove the 4th column
$row->children(3)->outertext = '';
// remove the 5th column
$row->children(4)->outertext = '';
}
print $html; // Outputs the table without 4th and 5th column of each row;
您可以在此处阅读相关答案Simple HTML Dom: How to remove elements?