dozc1071 2013-07-17 04:34 采纳率: 100%
浏览 36
已采纳

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

im using simple html dom parser to get scrap some data from html string .
i need to return the TD's values from a table with specific css class each TD as array element
i tried this cod but it gives fetal error

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

then i tried

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

the last one worked good but it brings all TD;s as a single string no each td as an array element ? var_dump result

Array ( 
[0] => 3571 Premium GD 2063199876 Washington Seattle 3703 
)
  • 写回答

1条回答 默认 最新

  • duanke0178 2013-07-17 04:43
    关注

    Change your query from

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

    to

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

    This should allow you to iterate through all of the td's instead of getting the plain text of the whole row.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 outlook无法配置成功
  • ¥15 Pwm双极模式H桥驱动控制电机
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换