Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting li items from a html file using php

I have an HTML file that contains many many JUST "li" tags no head and body tag and any thing else. I want to count them using PHP. how can I do this?

However, I tried this:

$dom = new DOMDocument();
DOMDocument::loadHTML($tmp_file);
$count = $dom->getElementsByTagName("li");
echo count($count);

but it returns 1.

Here is the $tmp_file (I don't know how many is them will be retrieved (may be hundred of them) but I just add 5 of them to here):

<li >
    <a target="_blank" class="small-news-link" href="http://www.varzesh3.com/news/1426832/میروسلاو-ژیوکو-سرمربی-تیم-والیبال-سایپا-شد" target="_blank" title="میروسلاو ژیوکو سرمربی تیم والیبال سایپا شد">میروسلاو ژیوکو سرمربی تیم والیبال سایپا شد</a>
</li>
<li >
    <a target="_blank" class="small-news-link" href="http://www.varzesh3.com/news/1426824/فدرر-از-نظر-فیزیکی-شرایط-سال-قبل-را-ندارم" target="_blank" title="فدرر: از نظر فیزیکی شرایط سال قبل را ندارم">فدرر: از نظر فیزیکی شرایط سال قبل را ندارم</a>
</li>
<li >
    <a target="_blank" class="small-news-link" href="http://www.varzesh3.com/news/1426817/شکست-تیم-&#171;الف&#187;-والیبال-ساحلی-ایران-مقابل-هلند" target="_blank" title="شکست تیم &#171;الف&#187; والیبال ساحلی ایران مقابل هلند">شکست تیم &#171;الف&#187; والیبال ساحلی ایران مقابل هلند</a>
</li>
<li class="news-video">
    <a target="_blank" class="small-news-link" href="http://www.varzesh3.com/news/1426815/5-حرکت-دیدنی-در-لیگ-تابستان-NBA؛-96-04-21" target="_blank" title="5 حرکت دیدنی در لیگ تابستان NBA؛ 96/04/21">5 حرکت دیدنی در لیگ تابستان NBA؛ 96/04/21</a>
</li>
<li >
    <a target="_blank" class="small-news-link" href="http://www.varzesh3.com/news/1426813/معرفی-هیات-مدیره-جدید-صندوق-حمایت-از-پیشکسوتان" target="_blank" title="معرفی هیات مدیره جدید صندوق حمایت از پیشکسوتان">معرفی هیات مدیره جدید صندوق حمایت از پیشکسوتان</a>
</li>
<li >
    <a target="_blank" class="small-news-link" href="http://www.varzesh3.com/news/1426808/رحیمی،-یزدانی-و-قاسمی-در-رده-اول-تا-سوم-جهان" target="_blank" title="رحیمی، یزدانی و قاسمی در رده اول تا سوم جهان">رحیمی، یزدانی و قاسمی در رده اول تا سوم جهان</a>
</li>
<li >
    <a target="_blank" class="small-news-link" href="http://www.varzesh3.com/news/1426792/جوکوویچ-منتظر-رویارویی-با-بردیچ-هستم" target="_blank" title="جوکوویچ: منتظر رویارویی با بردیچ هستم">جوکوویچ: منتظر رویارویی با بردیچ هستم</a>
</li>
like image 618
amdev Avatar asked Jul 12 '17 08:07

amdev


People also ask

How to count number of files in a directory using PHP?

How to count files in a directory using PHP? PHP contains many functions like count (), iterator_count (), glob (), openddir (), readdir (), scandir () and FilesystemIterator () to count number of files in a directory.

How to count total count of items in HTML list?

But the total count of items in HTML list was to be displayed on the webpage. Ordered HTML list (created with OL tag) are by default counted as each item is preceded by its number in the list. But the lists created using UL tag are not numbered.

How to count the total number of items in a loop?

When the loop is done, counter variable would contain the total number of items. Then you can print this count wherever you want on your web page. Paste the following JavaScript code in the HEAD section of your page: And now place the following code at the place where you want to display the count.

How to print the Count of a list in JavaScript?

Then you can print this count wherever you want on your web page. Paste the following JavaScript code in the HEAD section of your page: And now place the following code at the place where you want to display the count. Make sure that you replace ‘my-html-list’ with the id of your list. You can have any ID for your list.


Video Answer


2 Answers

You're close. I think what you're looking for is the following :

$dom = new \DOMDocument();
@$dom->loadHTML($html); // or @$dom->loadHTMLFile($filename); if providing filename rather than actual HTML content

$count = $dom->getElementsByTagName('li')->length;
echo $count;

Depending on your value of $tmp_file you would use either loadHTML() if it contains the actual content, or loadHTMLFile() if it contains the filename. (Note that these methods should not be called statically.)

The method getElementsByTagName() returns a DOMNodeList object with a length property containing the number of found nodes.

You can try the code here.

This DOM parsing approach is preferable to string or regular expression searches since it is designed to account for the many variable ways that HTML may be acceptably written (ie. inconsistent spacing, attribute order).

like image 87
Francis Eytan Dortort Avatar answered Oct 06 '22 18:10

Francis Eytan Dortort


You can do a very simple Substring Count for <li> (or -li-) on that string and it would return the number of items. See here: function.substr-count

$count = substr_count($html,'<li>'); //where $html holds your piece of HTML.
like image 43
ankit suthar Avatar answered Oct 06 '22 16:10

ankit suthar