12

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>
amdev
  • 6,703
  • 6
  • 42
  • 64

12 Answers12

8

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

Francis Eytan Dortort
  • 1,407
  • 14
  • 20
  • as said above , length in not works it should be length() , by the way , thanx man, i'll post my answer her as soon as possible – amdev Sep 12 '17 at 05:52
  • It should indeed be `->length` and not `->length()` since `length` is a _property_ of `DOMNodeList`, not a _method_, and properties are accessed without the parentheses. If you're still not getting the desired output, than the issue is elsewhere, possibly the use of `loadHTML()` instead of `loadHTMLFile()`? – Francis Eytan Dortort Sep 12 '17 at 12:13
5

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.
always-a-learner
  • 3,671
  • 10
  • 41
  • 81
  • 1
    still zero buddy (with extra space)! – amdev Jul 12 '17 at 08:40
  • 2
    @Er.Ellison `substr_count($html,'
  • ');` works perfectly for me when I try with the HTML in your question. Of course, you need to change `$html` to the variable that actually holds your HTML data. Try it: https://3v4l.org/daX97
  • – M. Eriksson Jul 12 '17 at 08:43
  • 2
    If you are going to go this route, I would recommend checking for `` or `
  • – Jo. Sep 11 '17 at 16:18