This function is used for leading out all tags with their counts from the file.

<?php
function tags_in_the_file($file){
    if(file_exists($file)){
        $content = file_get_contents($file, true);
 
        preg_match_all("/(<([\w]+)[^>]*>)/", $content, $matches, PREG_SET_ORDER);
 
        $tags = array();
        $tags_count = 0;
 
        foreach($matches as $tag){
            $tag_found = false;
 
            if(!empty($tags)){
                for($i=0; $i<count($tags); $i++){
                    if($tags[$i]["tag"]==$tag[2]){
                        $tags[$i]["count"]++;
                        $tag_found = true;
                        break;
                    }
                }
            }
 
            if(!$tag_found){
                $tags[$tags_count]["tag"] = $tag[2];
                $tags[$tags_count]["count"] = 1;
 
                $tags_count++;
            }else{
                $tag_found = false;
            }
        }
 
        return $tags;
    }else{
      return false;
    }
}
 
$file = "test.html";
 
$tags = tags_in_the_file($file);
 
if($tags!==false){
    foreach($tags as $tag){
        echo("<p>".$tag["tag"]." - ".$tag["count"]."</p>");
    }
}else{
    echo("File not found!");
}
?>

the test.html content:

<html>
 
<head>
  <title>Tags of the file content</title>
</head>
 
<body>
 
<span id="hello">Hello</span> <span id="world">world</span>.
 
<p>My personal website is <a href="http://rubensargsyan.com">http://rubensargsyan.com</a></p>
 
</body>
 
</html>

Output:

html – 1

head – 1

title – 1

body – 1

span – 2

p – 1

a – 1


If the file is not found the function return false.

Share

Leave a Reply

Info