Home » » Count all HTML tags of a webpage in php

Count all HTML tags of a webpage in php

Written By Unknown on Kamis, 18 Oktober 2012 | 00.58

Count all HTML tags of a webpage using Curl and Html Dom. Curl is used to read the webpage and Html Dom for counting all tags.


<?php
$url="http://altafphp.blogspot.in";
$agent="Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/532.4 (KHTML, like Gecko) Chrome/4.0.233.0 Safari/532.4";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_MAXREDIRS, 2);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$data = curl_exec($ch);
curl_close($ch);

libxml_use_internal_errors(true);
$dom = new DOMDocument;
$dom->loadHTML($data);
$tags = $dom->getElementsByTagName('*');
echo "Total Tags : ". $tags->length . "<br /><br />";

$count_tag = array();
foreach($tags as $tag) {
if(array_key_exists($tag->tagName, $count_tag)) {
$count_tag[$tag->tagName] += 1;
} else {
$count_tag[$tag->tagName] = 1;
}
}
echo "<pre>";
print_r($count_tag);
echo "</pre>";
?>


The output would be something like this


Total Tags : 863

Array
(
[html] => 1
[head] => 1
[meta] => 9
[script] => 30
[link] => 13
[title] => 1
[style] => 2
[body] => 1
[div] => 272
[header] => 1
[h1] => 1
[p] => 1
[span] => 151
[a] => 151
[img] => 24
[h3] => 7
[abbr] => 7
[br] => 76
[pre] => 10
[plusone] => 7
[b] => 22
[ol] => 1
[li] => 39
[aside] => 2
[h2] => 4
[dl] => 1
[dt] => 1
[dd] => 1
[ul] => 25
[footer] => 1
)

Share this article :

Posting Komentar

 
Support : Creating Website | Johny Template | Mas Template
Copyright © 2011. Kumpulan Kata Broadcast Blackberry - All Rights Reserved
Template Created by Creating Website Published by Mas Template
Proudly powered by Blogger