simple_html_dom: why is the $html (string which contains the table to be parsed) empty?
我有一个字符串,其中包含HTML表的HTML。我想从表中提取数据作为维数组。类似于:
1 2 3 4 |
我尝试了很多方法,但是它们一直给我错误。现在,我正在使用" simple_html_dom",它似乎很容易理解。因此,我将使用它。
我正在尝试使用该问题的可接受答案中给出的代码。但这给了我
我搜索并找到了解决方案,但是当我放入支票(在下面的代码中注释)时,我得到了
代码:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | <?php require('simple_html_dom.php'); $html = 'Edit question<h4 class="accesshide">Question text</h4><input type="hidden" name="q18:1_:sequencecheck" value="1" /><table style="width: 454px; height: 269px;" border="1"><caption> </caption> <tbody> <tr> <td>Name</td> <td>Age</td> <td>CGPA</td> </tr> <tr> <td>Alice</td> <td>24</td> <td>4</td> </tr> <tr> <td>Bob</td> <td>14</td> <td>3</td> </tr> <tr> <td>Amy</td> <td>33</td> <td>2</td> </tr> </tbody> </table> <p> </p> <p>Blah BlahBlahBlahBlahBlahBlahBlahBlahBlahBlahBlahBlah BlahBlahBlahBlahBlahBlahBlahBlahBlahBlahBlahBlahBlahBlahBlahBlahBlahBlah?</p>Select one:<input type="radio" name="q18:1_answer" value="0" id="q18:1_answer0" /><label for="q18:1_answer0">a. [1]ir[/1][2]34[/2]</label> '; //if (!empty($html)) { // get the table. Maybe there's just one, in which case just 'table' will do $table = $html->find('table'); //} else {die '$html is empty!';} // initialize empty array to store the data array from each row, that is the array containing the rows (that is entire <tr> tag). $rowData = array(); // loop over rows foreach($table->find('tr') as $row) { // initialize array to store the cell data from each row, that is the arrays containing data from <td> tags $cellData = array(); foreach($row->find('td.text') as $cell) { // push the cell's text to the array $cellData[] = $cell->innertext; } // push the row's data array to the 'big' array $rowData[] = $rowData; } print_r($rowData); |
您可以直接将其指向表格行。示例:
1 2 3 4 5 6 7 8 9 10 11 | $html_string = 'Edit question<h4 class="accesshide">Question text</h4><input type="hidden" name="q18:1_:sequencecheck" value="1" /><table style="width: 454px; height: 269px;" border="1"><caption> </caption><tbody><tr><td>Name</td><td>Age</td><td>CGPA</td></tr><tr><td>Alice</td><td>24</td><td>4</td></tr><tr><td>Bob</td><td>14</td><td>3</td></tr><tr><td>Amy</td><td>33</td><td>2</td></tr></tbody></table><p> </p><p>Blah BlahBlahBlahBlahBlahBlahBlahBlahBlahBlahBlahBlah BlahBlahBlahBlahBlahBlahBlahBlahBlahBlahBlahBlahBlahBlahBlahBlahBlahBlah?</p>Select one:<input type="radio" name="q18:1_answer" value="0" id="q18:1_answer0" /><label for="q18:1_answer0">a. [1]ir[/1][2]34[/2]</label> '; $html = str_get_html($html_string); // load the string $rowData = array(); foreach($html->find('table tr') as $row_key => $row) { // load each row foreach($row->children() as $td) { // for every td $rowData[$row_key][] = $td->innertext; // push the each td in that row } } echo '[cc lang="php"]'; print_r($rowData); |
应该这样输出:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
您的代码说明:
1 | $table = $html->find('table'); |
您还不能调用