简单的html dom解析器返回所有td的值?

时间:2013-07-17 04:34:11

标签: php php-5.3

我使用简单的html dom解析器从html字符串中删除一些数据 我需要从具有特定css类的表中返回TD的值,每个TD作为数组元素
我试过这个鳕鱼,但它给出了胎儿错误

<?php
include('classes/simple_html_dom.php');
$html = str_get_html('<table class="pages_navigator">
<tr>
<th style="width:50px;">ID </th>
<th>Business </th>
<th style="width:70px;">Category</th>
<th style="width:50px;">Phone </th>
<th style="width:70px;">State</th>
<th style="width:70px;">City</th>
<tr class="users_tr">
<td>3571</td>
<td>Premium GD</td>
<td>2063199876</td>
<td>Washington</td>
<td>Seattle</td>
<td>3703</td>
</tr>
</table>');
$tds = $html->find('table.pages_navigator')->find('td') ;
print_r($tds);
?>
然后我试了

<?php
    include('classes/simple_html_dom.php');
    $html = str_get_html('<table class="pages_navigator">
    <tr>
    <th style="width:50px;">ID </th>
    <th>Business </th>
    <th style="width:70px;">Category</th>
    <th style="width:50px;">Phone </th>
    <th style="width:70px;">State</th>
    <th style="width:70px;">City</th>
    <tr class="users_tr">
    <td>3571</td>
    <td>Premium GD</td>
    <td>2063199876</td>
    <td>Washington</td>
    <td>Seattle</td>
    <td>3703</td>
    </tr>
    </table>');
    $result = array();
    foreach($html->find('tr.users_tr') as $e){
    $result[] = $e->plaintext . '<br>';
    }
    print_r($result);
?>

最后一个工作得很好但是它带来了所有TD; s作为单个字符串没有每个td作为数组元素? var_dump结果

Array ( 
[0] => 3571 Premium GD 2063199876 Washington Seattle 3703 
)

1 个答案:

答案 0 :(得分:2)

更改您的查询
foreach($html->find('tr.users_tr') as $e){

foreach($html->find('tr.users_tr td') as $e){

这应该允许您遍历所有td而不是获取整行的纯文本。