2013-07-17 48 views
0

即時通訊使用簡單的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)? –

回答

2

變化從

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){ 謝謝 –

相關問題