What are Tag Clouds?
In technical terms “Tag cloud” can be defined as visual depiction of user-generated tags (words), used typically to describe the content of that page or the website. Not so long ago, we used “meta” tags to describe and define the html page and its contents. In the current time, tag clouds have replaced “meta” tags more than one reason.
Why use Tag Clouds?
The number one reason is SEO (Search engine optimization). Modern day search engines like Google do not bank only on the “Meta” tags to index a web page. They index pages based on its content as well as the links on that page. Apart from SEO, it helps providing a better user experience and each tag (word) is a link to that actual content.
How to generate Tag Clouds?
The idea is to automate the process of Tag Cloud generation as soon as user writes a post or content on a page. It can be divided into two steps:
- Generate Tag Clouds from the content.
Ideally, we would like PHP code to extract relevant tags (keywords) from the content. We do not want common words like “is”, “this”, “the” to be keywords. We can set up our own set of rules depending on the requirement.
Sample PHP code:
function generateTagCloud($content){
$minTagLength=5;
$maxTagLength=14;
$baseFont=16;
$returnArray=array();
$content=str_replace("\r\n"," ",$content);
$content=strip_tags($content);
$tagArray=explode(" ",$content);
for($i=0;$i=$minTagLength && $len< =$maxTagLength) {
$returnArray[]=$tag;
}
}
$returnArray=array_count_values($returnArray);
arsort($returnArray);
ksort($returnArray);
return $returnArray;
}
- Display Tags according to their occurrence.
After we have all the tags (keywords) we need to display then according their occurrence or presence in the content. In other words, the tag which is present more times in the content will be displayed in bigger fonts as the rest.
Sample PHP code to achieve this is as follows:
function displayTagCloud($tagCloudArray){
$buffer="";
foreach($tagCloudArray as $tags=>$key) {
$font=($baseFont+($key*10));
$font=$font."px";
$buffer.=" $tags ";
}$buffer.="";
return $buffer;
}
Screenshot of final output:

