How to Extract Headings tag from Website URL in PHP

Authors: CodeToday | PHP Code | Views: 568 | Posted: 04 AM: 09/19/2017

Next article Extract Title and Meta Tags from Website URL using PHP Code. Today I will guide you how to extract headings tag from a website url using PHP code.


<form action="" method="post" class="form-inline">
<div class="form-group">
<input type="text" name="url" id="url" value="<?=$_POST['url']?>" size="80" class="form-control" placeholder = "Enter website url here ..." required/>
<div class="form-group">
<button class="btn btn-info" type="submit" name="submit"><i class="glyphicon glyphicon-search"></i> Extract Headings Tag Now</button>

get_headings_tag () function as follow:

function get_headings_tag($html) {
	$headings = array(
			'h1' => array(),
			'h2' => array(),
			'h3' => array(),
			'h4' => array(),
			'h5' => array(),
			'h6' => array(),
	$pattern = "<(h[1-6]{1})(.+)?>(.*)</h[1-6]{1}(?:[^>]*)>";
	preg_match_all("#{$pattern}#iUs",$html, $matches);
	$sizes = isset($matches[1]) ? $matches[1] : array();
	foreach($sizes as $id => $size) {
		$headings[strtolower($size)][] = strip_tags(trim($matches[3][$id]));
	return $headings;

PHP Source Code

if(isset($_POST['submit'])) {
	$url   = trim($_POST['url']);
	$data  = curlGet($url);	
	$tags  = get_headings_tag($data);
	$htags = array('h1','h2','h3','h4','h5','h6');
	echo '<h3 class="text-primary" style="margin-top:5px">H Tags</h3>';
	echo '<div class = "row">';
	foreach($htags as $htag) { 	
		echo '<div class="col-sm-6">&nbsp;&nbsp;<strong>'.ucfirst($htag).' ('.count($tags[$htag]).')</strong>';
		echo '<ul>';
			foreach($tags[$htag] as $tag[$htag]) {
				echo '<li>'.$tag[$htag].'</li>';
		echo '</ul></div>';
	echo '</div>';


function curlGet($url){
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_HTTPGET, 1 );
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION , 1 );
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION , 1 );
	curl_setopt($ch, CURLOPT_URL, $url );
	curl_setopt($ch, CURLOPT_REFERER, $ref );
	curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);     
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
	$data = curl_exec($ch);
	return $data;

After entering a website url, try the following image:

How to Extract Headings tag from Website URL in PHP

If you have any questions, please leave a message below