Find and Count All Headings From a Web Page in PHP

Reading Time: 4 minutes
3,474 Views

Inside this article we will see the concept of find and count all headings from a web page in php. Concept of this article will provide very classified information to understand the things.

This PHP tutorial is based on how to find and count all headings from a web page. In this guide, we will see how to fetch the HTML content of a web page by URL and then extract all the headings and count from it. To do this, we will be use PHP’s DOMDocument class.

Header tags, also known as heading tags, are used to separate headings and subheadings on a webpage. They rank in order of importance, from H1 to H6, with H1s usually being the title. Header tags improve the readability and SEO of a webpage.

HTML headings tags from H1 to H6. Like

<h1>...</h1>
<h2>...</h2>
...
<h6>...</h6>

DOMDocument of PHP also termed as PHP DOM Parser. We will see step by step concept to find and extract all headings from a html using DOM parser.

Learn More –

Let’s get started.


Find & Count All Headings From a Web Page

Inside this example we will use web page URL to get all headings and count their numbers.

Create file index.php inside your application.

Open index.php and write this complete code into it.

<?php 

$htmlString = file_get_contents('https://onlinewebtutorblog.com/');

//Create a new DOMDocument object.
$htmlDom = new DOMDocument;

//Load the HTML string into our DOMDocument object.
@$htmlDom->loadHTML($htmlString);

//Extract all h1 elements / tags from the HTML.
$h1Tags = $htmlDom->getElementsByTagName('h1');

//Extract all h2 elements / tags from the HTML.
$h2Tags = $htmlDom->getElementsByTagName('h2');

//Extract all h3 elements / tags from the HTML.
$h3Tags = $htmlDom->getElementsByTagName('h3');

//Extract all h4 elements / tags from the HTML.
$h4Tags = $htmlDom->getElementsByTagName('h4');

//Extract all h5 elements / tags from the HTML.
$h5Tags = $htmlDom->getElementsByTagName('h5');

//Extract all h6 elements / tags from the HTML.
$h6Tags = $htmlDom->getElementsByTagName('h6');


echo "Total H1 Tags: ". count($h1Tags)."<br/>";
echo "Total H2 Tags: ". count($h2Tags)."<br/>";
echo "Total H3 Tags: ". count($h3Tags)."<br/>";
echo "Total H4 Tags: ". count($h4Tags)."<br/>";
echo "Total H5 Tags: ". count($h5Tags)."<br/>";
echo "Total H6 Tags: ". count($h6Tags);

Output

When we run index.php. Here is the output

We hope this article helped you to Find and Count All Headings From a Web Page in PHP Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more