simple html dom parser to return all td`s values?

别说谁变了你拦得住时间么 提交于 2019-12-13 06:29:04

问题


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:


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.



来源:https://stackoverflow.com/questions/17691175/simple-html-dom-parser-to-return-all-tds-values

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!