简单 HTML Dom - 仅解析原始 table 中的几列
Simple HTML Dom - parse only few columns from original table
我想复制一些 table 到我的网站,但只有几个特定的栏目
这是我的代码
<?php
require('simple_html_dom.php');
$html = file_get_html('http://pomorskifutbol.pl/liga.php?id=970');
$table = $html->find('table', 0);
$rowData = array();
foreach($table->find('tr') as $row) {
// initialize array to store the cell data from each row
$flight = array();
foreach($row->find('td') as $cell) {
// push the cell's text to the array
$flight[] = $cell->plaintext;
}
$rowData[] = $flight;
}
echo '<table>';
foreach ($rowData as $row => $tr) {
echo '<tr>';
foreach ($tr as $td)
echo '<td>' . $td .'</td>';
echo '</tr>';
}
echo '</table>';
?>
whole table
这会打印整个 table,我只想得到几列,例如仅 "M."、"Drużyna"、"M"、"PKT"
如何从此 table 中仅提取此列?
您可以更改条目:
foreach($row->find('td') as $cell) {
到
foreach($row->find('td') as $columnNumber => $cell) {
并在填充 $flights 数组之前做一个 if,例如
//somewhere in code
$columnNumbers = [ 0, 2, 3,4, 7];
// int the foreach loop
if ( in_array( $columnNumber, $columnNumbers ) ) {
$flight[] = $cell->plaintext;
}
PS。波沃兹尼亚 :)
我想复制一些 table 到我的网站,但只有几个特定的栏目
这是我的代码
<?php
require('simple_html_dom.php');
$html = file_get_html('http://pomorskifutbol.pl/liga.php?id=970');
$table = $html->find('table', 0);
$rowData = array();
foreach($table->find('tr') as $row) {
// initialize array to store the cell data from each row
$flight = array();
foreach($row->find('td') as $cell) {
// push the cell's text to the array
$flight[] = $cell->plaintext;
}
$rowData[] = $flight;
}
echo '<table>';
foreach ($rowData as $row => $tr) {
echo '<tr>';
foreach ($tr as $td)
echo '<td>' . $td .'</td>';
echo '</tr>';
}
echo '</table>';
?>
whole table 这会打印整个 table,我只想得到几列,例如仅 "M."、"Drużyna"、"M"、"PKT" 如何从此 table 中仅提取此列?
您可以更改条目:
foreach($row->find('td') as $cell) {
到
foreach($row->find('td') as $columnNumber => $cell) {
并在填充 $flights 数组之前做一个 if,例如
//somewhere in code
$columnNumbers = [ 0, 2, 3,4, 7];
// int the foreach loop
if ( in_array( $columnNumber, $columnNumbers ) ) {
$flight[] = $cell->plaintext;
}
PS。波沃兹尼亚 :)