Php 如何使用simple_html_dom或dom文档跳过最后n行?
有没有办法通过simple_html_dom或dom文档始终跳过已解析表的最后n行 我尝试使用固定的行号,但由于源文件可以更改其行数,因此没有成功 这是我解析表的标准代码。你有什么想法或提示给我,如何总是跳过最后两行Php 如何使用simple_html_dom或dom文档跳过最后n行?,php,html-table,html-parsing,simple-html-dom,skip,Php,Html Table,Html Parsing,Simple Html Dom,Skip,有没有办法通过simple_html_dom或dom文档始终跳过已解析表的最后n行 我尝试使用固定的行号,但由于源文件可以更改其行数,因此没有成功 这是我解析表的标准代码。你有什么想法或提示给我,如何总是跳过最后两行 $table = $html->find('table', 1); $rowData = array(); foreach($table->find('tr') as $row) { // initialize array to store t
$table = $html->find('table', 1);
$rowData = array();
foreach($table->find('tr') as $row) {
// initialize array to store the cell data from each row
$roster = array();
foreach($row->find('td') as $cell) {
$roster[] = $cell->innertext;
}
foreach($row->find('th') as $cell) {
$roster[] = $cell->innertext;
}
$rowData[] = $roster;
}
foreach ($rowData as $row => $tr) {
echo '<tr>';
foreach ($tr as $td)
echo '<td>' . $td .'</td>';
echo '</tr>';
}
echo '</table></td><td>';
$table=$html->find('table',1);
$rowData=array();
foreach($table->find('tr')作为$row){
//初始化数组以存储每行的单元格数据
$LOSTER=数组();
foreach($row->find('td')作为$cell){
$LOSTER[]=$cell->innertext;
}
foreach($row->find('th')作为$cell){
$LOSTER[]=$cell->innertext;
}
$rowData[]=$LOSTER;
}
foreach($rowdataas$row=>$tr){
回声';
foreach($tr as$td)
回音“.$td.”;
回声';
}
回声';
您只需从结果数组中查找两项即可:
$rows = $table->find('tr');
array_pop($rows);
array_pop($rows);
foreach ($rows as $row) {
// do stuff here
}
当然,这不是一个理想的解决方案,作为替代方案,您可以获得找到的行的计数
,并使用索引控制foreach
中的当前元素:
$rows = $table->find('tr');
$limit = count($rows) - 2;
$counter = 0;
foreach ($rows as $row) {
if ($counter++ < $limit) {
break;
}
// do stuff
}
$rows=$table->find('tr');
$limit=计数($rows)-2;
$counter=0;
foreach($行作为$行){
如果($counter++<$limit){
打破
}
//做事
}
谢谢。使用array\u pop
并按要求交付。反之亦然,它将如何工作?假设我只想显示最后两行,并跳过前面的所有行。或者array\u pop
返回弹出的元素,这样您就可以pop
它们并输出。