2015-10-18 48 views
0

使用simpel dom parser來檢索som數據。但是,當我改變表格佈局解析器停止工作。我得到錯誤PHP Fatal error: Call to a member function find() on null解析表並刪除多餘數據

require('simple_html_dom.php'); 
$html = file_get_html($url); 
$table = $html->find('table'); 
$rowData = array(); 

foreach($table->find('tr') as $row) { 
    // initialize array to store the cell data from each row 
    $stocks = array(); 
    foreach($row->find('td') as $cell) { 
     // push the cell's text to the array 
     $stocks[] = $cell->plaintext; 
    } 
    $rowData[] = $stocks; 
} 

echo '<table>'; 
foreach ($rowData as $row => $tr) { 
    echo '<tr>'; 
    foreach ($tr as $td) 
     echo '<td>' . $td .'</td>'; 
    echo '</tr>'; 
} 
echo '</table>'; 

看看這個pastebin(對不起大桌子佈局)。正是從這個表我想提取下列個

+--------+--------+------+----+------+-------+--------+--------+---------+ 
| Aktie | Senast | +/- | % | Köp | Sälj | Högst | Lägst | Omsatt | 
+--------+--------+------+----+------+-------+--------+--------+---------+ 

這snipp的作品,但它並沒有在表中安排數據:

require('simple_html_dom.php'); 
$html = file_get_html($url); 

// remove all image 
foreach($html->find('img') as $e) 
    $e->outertext = ''; 

// Remove a attribute, set it's value as null! 
foreach($html->find('a') as $e) 
$e->href = null; 

// Find all <td> in <table> which class=hello 
foreach($html->find('table tr') as $es) 
echo $es->innertext . '<br>'; 

我的問題:

  • 我如何獲取上述th?並從td列中插入相應的數據?

預期的結果是:

+-------------+--------+-------+-------+-------+-------+--------+--------+---------+ 
| Aktie  | Senast | +/- | % | Köp | Sälj | Högst | Lägst | Omsatt | 
+-------------+--------+-------+-------+-------+-------+--------+--------+---------+ 
| AAK AB  | 549,90 | ..etc | ..etc | ..etc | ..etc | ..etc | ..etc | ..etc | 
| ABB LTD  | 149.80 | ..etc | ..etc | ..etc | ..etc | ..etc | ..etc | ..etc | 
| and so on.. |  |  |  |  |  |  |  |   | 
+-------------+--------+-------+-------+-------+-------+--------+--------+---------+ 

回答