php解析html表并计数特定的< td>类似于另一个 [英] Php parsed html table and count specific <td> similar to another

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

问题描述

这个问题紧随另一个问题,刚刚解决了此处
现在,我想做一个不同的计数,更难弄清.

在我解析的HTML表中,每一行都包含两个非常相似且相应的'td'(数字4和5 ):

This question follows another, just solved here
Now I want to do a different count, more difficult to figure out.

In my parsed HTML table, every rows contains two very similar, and consequential, 'td' (number 4 and 5):

    <tr>
(1) <td class="tdClass" ....</td>
(2) <td class="tdClass" ....</td>
(3) <td class="tdClass" ....</td>
(4) <td class="tdClass" align="center" nowrap="">No</td>
(5) <td class="tdClass" align="center" nowrap="">No</td>
    </tr>

第一个字母"td"中的字符串可以是"No",第二个字母中的字符串可以是"Yes",反之亦然,可以是"Yes",也可以都是"No".

The strings could be "No" in the first 'td' and "Yes' in the second one, and vice versa, both "Yes" or both "No".

我要计算5种类型的"td"中是否包含否"

到现在为止,我通过循环遍历其他"td"来计算它们(请参阅顶部顶部链接的我的上一个问题的选定答案),并仅选择与目标字符串匹配的一个. 可以这样做是因为这些目标字符串在每一行中只出现一次.

在这种情况下,相反,目标字符串(否")并不是每一行都是唯一的,因为如上例所示,可以在同一'tr'中存在两次(在'td'4& 5中).

这样,我真的不知道如何只为每一行选择与目标字符串"No"匹配的第二(5)个"td",并排除(4)"td"(可以匹配相同的字符串).字符串).

显然,这两个'td'-在不同的列标题下,但这对区分它们是没有用的.

我想到的唯一解决方案是从左数起'td'位置,并仅选择第5个,但我不知道是否可能.

I want to count how many 'td', of the 5 kind, contains "No"

By now I counted other 'td'-s by looping through them (see the selected answer for my previous question linked at the top) and selecting only ones matching a target string.
This could be done because those target strings appear only one time in each row.

In this case, instead, the target string ("No") is not unique for each row, because, as in the example above, could exist two times (in the 'td' 4 & 5) in the same 'tr'.

By that, I really don't know how to select only the second (5) 'td' for each row, which match the target string "No", and to exclude the (4) 'td' (which could match the same string) from the count.

Obviously, these two 'td'-s are under different column titles, but this couldn't be useful to distinguish them.

The only solution I have thought about, is to count the 'td' position from left, and to select only the 5th one, but I don't know if it's possible.

推荐答案

您确实必须更新某些部分.首先,您将需要第4个和第5个元素,因此必须进行检查(保留计数器或使用for循环).其次,在这种情况下,您不需要中断,因为它可以停止循环.

You will indeed have to update some parts. First you will want the 4th and 5th element, so you'll have to check for that (keep a counter or use a for loop). Secondly you don't need the break in this case, since it stops the loop.

代码:

<?php

$targetString = 'No';
$rows = $table->find('.trClass');

$count = 0;
foreach($rows as $row) {
    $tds = $row->find('td');
    for (i = 0; i < count($tds); $i++) {
        // Check for the 4th and 5th element
        if (($i === 3 || $i === 4) && $tds[$i]->innertext === $targetString) {
            $count++;
        }
    }
}

在这里,我使用for循环而不是foreach循环,因为我不想手动保持计数器.我可以轻松地使用$i并将其用作索引.

Here I use a for loop instead of a foreach loop because I don't want to keep manually a counter. I can use the $i easily for this and just use it as an index as well.

这篇关于php解析html表并计数特定的&lt; td&gt;类似于另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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