将具有HTML DOM的HTML表解析为数组


Parse HTML table with HTML DOM to array

我正试图用php将HTML表放入数组中。我已经找到了一个带有代码的帖子,但如果我使用该代码,它将显示不正确此表(第二行/Dinsdag):

http://rooster.farelcollege.nl/16/s/s00156.htm输出:

Array
(
    [  Dinsdag     ] => 1
    [] => 1
    [  Veh.       ec4       276.     ] => 1
    [  Vec.       nsk1_       202.     ] => 1
    [  Vog       rkt       112.     ] => 1
    [  Sle       ma       173.     ] => 1
    [  Rem       wi       275.     ] => 1
    [  Wie       ne       172.     ] => 1
    [  Klo       en       276.     ] => 1
)

代码:

<html>
  <head>
    <title>TEST page</title>
  </head>
  <body>
  <?php
    require('simple_html_dom.php');
    $table = array();
    $html = file_get_html('http://rooster.farelcollege.nl/16/s/s00156.htm');
    foreach($html->find('tr') as $row) {
      $first = $row->find('td',0)->plaintext;
      $sec = $row->find('tr',2)->plaintext;
      $table[$sec] = true;
    }
    echo '<pre>';
    print_r($table);
    echo '</pre>';
  ?>
  </body>
</html>

如果你检查这是不对的?!

http://prntscr.com/6r5unj

我能得到这样的输出吗?

null
null
Veh. ec4 276.
Veh. ec4 276.
Vog rkt 112.
Sle ma  173.
Rem wi  275.
Wie ne  172.
Klo en  276.

请尝试以下操作:

 <html>
 <head>
 <title>TEST page</title>
 </head>
 <body>
 <?php
 require('simple_html_dom.php');
$table = array();
$html = file_get_html('http://rooster.farelcollege.nl/16/s/s00156.htm');
foreach($html->find('tr') as $row) {
  $first = $row->find('td',0)->plaintext;
  $sec = $row->find('tr',2)->plaintext.'<br>';
  $table[$sec] = true;
}
echo '<pre>';
echo $table;
echo '</pre>';
?>
</body>
</html>