How can I count a certain number of li tags using PHP? [closed]
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.
精彩评论