doujilou3903 2012-05-03 21:08
浏览 25
已采纳

Php解析html dom并计算特定行

I'm using the "Simple php DOM Parser" to parse an html table and count its row.

I solved to count all the rows (tr) in it with this code:

$rows = $table->find('.trClass');
$count = count($rows);
echo $count;

And I correctly get the number of all the rows in the table.

Now I want to count only the rows which contains a specific td (with a specific string).
We could assume that I want to count only the rows with this td:

<td class="tdClass" align="center" nowrap="">TARGET STRING</td>

How can I modify the first code to match this scope?

I tried to use "preg_match" or "preg_match_all" but I don't have much experience in it, so I miss the correct syntax..I think.

Any help is very appreciated!

  • 写回答

2条回答 默认 最新

  • duanbo6482 2012-05-03 21:19
    关注

    How about:

    <?php
    $targetString = 'TARGET STRING';
    $rows = $table->find('.trClass');
    
    $count = 0;
    foreach($rows as $row) {
        foreach($row->find('td') as $td) {
            if ($td->innertext === $targetString) {
                $count++;
                break;
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题