Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count html tags by php in string

Tags:

php

Hi there I am requesting you to tell me the simple php script to count special html tag, like <p> in string. I had no idea to try this, since I am the beginner in php.

like image 489
Dipak Avatar asked Mar 26 '26 17:03

Dipak


1 Answers

You can make use of DOMDocument Class for that .

<?php
$html='<p>Hey hello</p><b>Hey this is bold tag</b><p>Another paragraph</p>';
$dom = new DOMDocument;
$dom->loadHTML($html);
echo $dom->getElementsByTagName('p')->length;// "prints" 2
like image 133
Shankar Narayana Damodaran Avatar answered Mar 29 '26 05:03

Shankar Narayana Damodaran



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!