如何使用简单的html dom打印表的单元格 [英] how to print cells of a table with simple html dom

查看:130
本文介绍了如何使用简单的html dom打印表的单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个html代码.我使用简单HTML Dom将数据解析为我自己的php脚本.

i have this html code. Im using Simple HTML Dom to parse the data into my own php script.

<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中的文本放入数组中,例如:

I need to get the text inside the TDs in an array, example:

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

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

我已经尝试了几种方法来实现这一目标,但是我在每种方法中都失败了.有人可以帮我吗?

I've tried several ways to get that but i've fail in each and everyone of them. Can someone give me a hand?

推荐答案

这应该做到:

// 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);

这篇关于如何使用简单的html dom打印表的单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆