简单的HTML dom解析器返回所有td的值? [英] simple html dom parser to return all td`s values?

查看:133
本文介绍了简单的HTML dom解析器返回所有td的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

im使用简单的html dom解析器从html字符串中获取一些数据.
我需要从具有特定css类的表中返回TD的值,每个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);
?>

然后我尝试了

<?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结果

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 
)

推荐答案

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

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

这应该允许您遍历所有td,而不是获得整个行的纯文本.

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

这篇关于简单的HTML dom解析器返回所有td的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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