douwen5985 2012-09-04 00:21
浏览 21
已采纳

Simple Dom Parser在表中获得跨度值

I am using Simple DOM Parser and I am trying to get some info from a dynamic table that will look like this:

<table />
<tr>
   <td class="histogram-msg top-row">5 star</td>
   <td class="top-row bar-cell"><span class="bar bar5" style="width:150px">&nbsp;</span>&nbsp;<span>55</span></td>
</tr>
<tr>
   <td class="middle-row histogram-msg">4 star</td>
   <td class="middle-row bar-cell"><span class="bar bar4" style="width:65px">&nbsp;</span>&nbsp;<span>24</span></td>
</tr>
<tr>
   <td class="middle-row histogram-msg">3 star</td>
   <td class="middle-row bar-cell"><span class="bar bar3" style="width:38px">&nbsp;</span>&nbsp;<span>14</span></td>
</tr>
<tr>
   <td class="middle-row histogram-msg">2 star</td>
   <td class="middle-row bar-cell"><span class="bar bar2" style="width:19px">&nbsp;</span>&nbsp;<span>7</span></td>
</tr>
<tr>
   <td class="bottom-row histogram-msg">1 star</td>
   <td class="bottom-row bar-cell"><span class="bar bar1" style="width:35px">&nbsp;</span>&nbsp;<span>13</span></td>
</tr>
</table>

I am trying grab the values in the table i.e. : 5 star | 55, 4 star | 24, etc.

I get this info by $ret = $html->find('.user-ratings'); and when I try to print the span tags it just give me a bunch of white space.

How can I grab the rating type and value from the table above?

  • 写回答

1条回答 默认 最新

  • duanlu9970 2012-09-04 01:06
    关注

    This will do what you're trying to do:

    <?php
    
    include('simple_html_dom.php');
    
    $f = file_get_html('s.html');
    
    foreach($f->find('td[class=histogram-msg]') as $n) {
    
    foreach($n->parent()->find('td[class=bar-cell]') as $p) {
        $ratings[] = array('stars' => $n->innertext, 
       'rating' => strip_tags(str_replace("&nbsp;", "", $p)));
        }
    }
    
    print_r($ratings);
    
    Array
    (
    [0] => Array
        (
            [stars] => 5 star
            [rating] => 55
        )
    
    [1] => Array
        (
            [stars] => 4 star
            [rating] => 24
        )
    
    [2] => Array
        (
            [stars] => 3 star
            [rating] => 14
        )
    
    [3] => Array
        (
            [stars] => 2 star
            [rating] => 7
        )
    
    [4] => Array
        (
            [stars] => 1 star
            [rating] => 13
        )
    
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分