如何使用PHP计算一定数量的li标签?


How can I count a certain number of li tags using PHP?

我想知道如何使用PHP计算一定数量的li标签,当我达到一定数量时做些什么?我使用foreach来输出列表项。

最简单的可能是:

$count = substr_count($html , "<li>");
$i = 0;
foreach (…) {
    echo '<li>';
    $i++;
    if ($i > $certainNumber) {
        doSomething();
    }
}

根据您的声明,请参阅下面涉及的foreach。这是答案的抽象形式,因为我不知道您的foreach与实际HTML的显示有什么关系,所以您必须将其与实际代码联系起来。

$c = 0;
foreach($html as $html_line)
    if (strpos($html_line, '<li>'))
        $c++;
// $c is equal to the number of li's in your html code.