使用HTML DOM将HTML表解析为数组

时间:2015-04-08 14:08:44

标签: php html dom

我试图用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.

1 个答案:

答案 0 :(得分:0)

请试试这个:

 <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>