开发者

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

开发者 https://www.devze.com 2023-03-18 04:28 出处:网络
开发者_运维百科 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form.
开发者_运维百科 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

I was wondering how can I count a certain number of li tags using PHP and when I reach a certain number do something? I'm using a foreach to output the list items.


Simplest would probably be:

$count = substr_count($html , "<li>");


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


See below as per your statement that a foreach is involved. This is an abstracted form of the answer seeing as I don't exactly know how your foreach relates to the displaying of the actual HTML, so you'll have to relate this back to your actual code.

$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.
0

精彩评论

暂无评论...
验证码 换一张
取 消