简单的HTML Dom - 只解析原始表

时间:2017-01-05 11:06:48

标签: php html dom html-parsing simple-html-dom

我想将一些表复制到我的网站,但只有少数特定列

这是我的代码

<?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 这打印整个表格,我想只获得几列,例如只有“M.”,“Drużyna”,“M”,“PKT” 如何从此表中仅提取此列?

1 个答案:

答案 0 :(得分:1)

您可以更改条目:

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。 Powodzenia:)