8

私はこのhtmlコードを持っています。Simple HTML Domを使用して、データを自分のphpスクリプトに解析しています。

<table>
    <tr>
        <td class="header">Name</td>
        <td class="header">City</td>
    </tr>
    <tr>
        <td class="text">Greg House</td>
        <td class="text">Century City</td>
    </tr>
    <tr>
        <td class="text">Dexter Morgan</td>
        <td class="text">Miami</td>
    </tr>
</table>

配列内のTD内のテキストを取得する必要があります。例:

$ array [0] = array('Greg House'、'Century City'); $ array [1] = array('Dexter Morgan'、'Miami');

私はそれを得るためにいくつかの方法を試しましたが、私はそれらのすべてで失敗しました。誰かが私に手を差し伸べてもらえますか?

4

3 に答える 3

14

これは行う必要があります:

// get the table. Maybe there's just one, in which case just 'table' will do
$table = $html->find('#theTable');

// initialize empty array to store the data array from each row
$theData = array();

// loop over rows
foreach($table->find('tr') as $row) {

    // initialize array to store the cell data from each row
    $rowData = array();
    foreach($row->find('td.text') as $cell) {

        // push the cell's text to the array
        $rowData[] = $cell->innertext;
    }

    // push the row's data array to the 'big' array
    $theData[] = $rowData;
}
print_r($theData);
于 2010-07-18T23:45:35.560 に答える
6

それは動作します..これを試してみてください

 include('simple_html_dom.php');
 $html = file_get_html('mytable.html');
 foreach($html->find('table tr td') as $e){
    $arr[] = trim($e->innertext);
  }

 print_r($arr);

属性も含めて、任意のhtmlタグからデータを取得できます...

于 2015-03-18T10:52:39.423 に答える
2

@lucia nie

これを行う必要があります:

// initialize empty array to store the data array from each row
$theData = array();

// loop over rows
foreach($html->find('#theTable tr') as $row) {

// initialize array to store the cell data from each row
$rowData = array();
foreach($row->find('td.text') as $cell) {

    // push the cell's text to the array
    $rowData[] = $cell->innertext;
}

// push the row's data array to the 'big' array
$theData[] = $rowData;
}
print_r($theData);
于 2013-01-25T10:13:42.440 に答える