i'm searching for a way to count dynamic li elements inside an ul in php (not js).
For example:
<ul>
<li> lorem </li>
<li> ipsum </li>
<li> dolor </li>
<li> sit </li>
</ul>
would return me the number 4. (am i too stupid to use proper code here?)
Is there some way to accomplish that in php?
Thanks in advance!
EDIT:
The markup is generated by a cms system, the count should be placed before the list, inside the template file.
In JavaScript, you can use the . getElementsByTagName() method to get all the <li> elements in <ul>. In-addition, you can use the . querySelectorAll() method also to get all the <li>.
JavaScript Code :$("button"). click(function(){ console. log($("li"). length); });
You can do a very simple Substring Count for <li>
(or -li-
) on that string and it would return the number of items.
Edit:
$count = substr_count($html,'<li>'); //where $html holds your piece of HTML.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With