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

问题描述:

即时通讯使用简单的HTML DOM解析器来从HTML字符串中获取一些数据。
我需要从表中具有特定的CSS类的每个TD返回TD的值作为数组元素
我想这鳕鱼,但它给胎儿错误简单的html dom解析器返回所有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 
) 
+0

请加什么打印$结果? –

+0

@VaheShadunts新增 –

+0

你可以var_dump($ e)? –

变化从

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

查询到

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

这应该允许您通过所有的TD的的迭代,而不是让整个行的纯文本。

+0

@drneo哪种变化有效......我将删除那些没有从答案中得出的结果。 – Orangepill

+0

第一个foreach($ html-> find('tr.users_tr td')as $ e){ 谢谢 –