php解析html dom并计数特定的行 [英] Php parse html dom and count specific rows

查看:129
本文介绍了php解析html dom并计数特定的行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用简单php DOM解析器来解析html表并对其行进行计数。

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

我解决了计数所有的行(tr)在其中与此代码:

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.

现在我只想计算包含特定 td (具有特定字符串)的行。
我们可以假设我只想计算这个td的行:

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>

如何修改第一个代码以匹配此范围?


我试图使用preg_match或preg_match_all,但我没有太多的经验,所以我想念正确的语法。我想。

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.

任何帮助非常感谢!

推荐答案

如何:

<?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;
        }
    }
}

这篇关于php解析html dom并计数特定的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆