Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - count li elements inside ul

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.

like image 841
michael Avatar asked Sep 13 '11 07:09

michael


People also ask

How do you find the number of Li in UL?

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

How can get Li count in jquery?

JavaScript Code :$("button"). click(function(){ console. log($("li"). length); });


1 Answers

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.
like image 65
thwd Avatar answered Oct 04 '22 18:10

thwd