Count all HTML tags of a webpage in php

Count all HTML tags of a webpage with PHP DOMDocument class.

  <?php
   $url="http://altafphp.blogspot.in";
     
   libxml_use_internal_errors(true);
   $dom = new DOMDocument;
   $dom->loadHTMLFile($url);
   $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
    )

Count all HTML tags of a webpage in php Count all HTML tags of a webpage in php Reviewed by JS Pixels on October 18, 2012 Rating: 5

1 comment:

Altaf Web. Powered by Blogger.