intelliproject logo

Location: Web development - PHP    License: The GNU Lesser General Public License

How to implement Tag Clouds in PHP

Posted by Rochak Chauhan

This is how to implement Tag Clouds in PHP

Skill: Beginner

Posted: 19/11/2008

Views: 745

Rating: 3.67 /5

Popularity: 1.75

Sign Up to vote for this article

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:

1.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:

2. 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:

Final output

TagClouds

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License

About the author

Rochak Chauhan

www.rochakchauhan.com/blog

Location: India
Home page: http://www.rochakchauhan.com

Posted by Alexandre Tedeschi - IMAGINACOM at 22/11/2008 20:45
Excellent article, congratulations!

Let's just remember that this algorythm may be used for a lot of other purposes where analysing a web-page content should be necessary!

Maybe (and I mean maaaaybe) this is similar to Google AdSense tool, where a software tries to understand what the page is talking about.

Can also be used for some kind of "understanding" user submissions on forums, or even whole conversations.

Anyway, excellent piece of code!
I'll keep an eye on your next articles.

Best regards.
-- Alexandre Tedeschi - IMAGINACOM
-- www.imaginacom.com
Posted by Rochak Chauhan at 22/11/2008 21:35
Hi Alexandre,

Thank you for your appreciation. I will continue to contribute more posts and articles.

Best Regards,
Rochak Chauhan.
www.dmwtechnologies.com
Posted by mikaelb at 31/12/2008 07:34
Couple of problems with the code in general:
First of all, some code is missing above the for-loop (i recon it's another loop).
Secondly you should have run the tags through more functions. Like strtolower(). As you can see from your screen shot, comedy appears two times; for non-capital C and for one with a capital. The same goes for other words in the tag cloud.

But it's good enough as an example.
Thank you for posting articles for us to read.
Posted by Said Bakr at 16/04/2009 15:53
Hi,
I could not able to understand this loop:
for($i=0;$i=$minTagLength && $len< =$maxTagLength) {
$returnArray[]=$tag; }

Sign up to post message on the article message board!