Generate your own tag cloud with this easy to use PHP class, simply include the class, instantiate a new object and then push your words into the cloud and voila, you have a tag cloud.
<?php
require_once 'classes/wordcloud.class.php';
$cloud = new wordCloud();
$cloud->addWord('php'); // Basic Method of Adding Keyword
$cloud->addWord(array('word' => 'google', 'url' => 'http://www.google.com')); // Advanced user method
$cloud->addString('List of words i would like to include');
$cloud->addWords('hello','world','how','are','foo','bar');
echo $cloud->showCloud();
?>
The clouds default ordering is by random, if you wanted to order the cloud by size or word from largest to smallest, this can be done as follows.
$cloud->orderBy('size','desc');
As mentioned above, you can arbitrarily add custom tags to pass through the tag cloud generator for use later on in your code. If you wanted to colour code the tags based on word, you could do the following.
<?php
$cloud = new wordCloud();
$cloud->addWord(array('word' => 'php', 'size' => 1, 'url' => 'http://www.php.net', 'colour' => '#FF0000'));
$cloud->addWord(array('word' => 'google', 'size' => 2, 'url' => 'http://www.google.com', 'colour' => '#00FF00'));
$cloud->addWord(array('word' => 'digg', 'size' => 3, 'url' => 'http://digg.com', 'colour' => '#0000FF'));
$cloud->addWord(array('word' => 'lotsofcode', 'size' => 4, 'url' => 'http://www.lotsofcode.com', 'colour' => '#FFFF00'));
$myCloud = $cloud->showCloud('array');
foreach ($myCloud as $cloudArray) {
echo ' <a href="'.$cloudArray['url'].'" style="color: '.$cloudArray['colour'].';" class="word size'.$cloudArray['range'].'">'.$cloudArray['word'].'</a> ';
}
?>
The above examples show the tag cloud wrapper is completly cusomisable and can be used in lots of different ways.
Not only can you generate a tag cloud, but you can customise the output if required to pass any information you like, such as href, link colours and more, check out the demo for more details on this.
Some of the features that are available are:
* Link and URL Assignment
* Additional attribute assignment e.g. title etc
* Add styles and colour options to each tag
* Set a limit to the amount of tags that are displayed
* String to cloud, parse a single string into a tag cloud instantly
* Ignore list, to remove particular words from the cloud. e.g. and, the, it, i
* Ordering by a selective field. e.g. word, size, colour
* Parse an argument seperated list of words
* add a string with a custom seperator
* format of word output. (Customise upper/lowercase words & set trimming of words. Single word and multi-word customisation.)
Within the demo, i have provided:
* Examples of intergrating with a MySQL Database
* Examples of intergrating with a Flat Text File
Tag cloud background info
Originally i created a tag cloud script and wrote up a tutorial on how to create one yourself, this was quite well received and from there i developed a newer version of the code base with lots more features, because this wasn't very well publicised, the original page that sat here was visited on many occasions with the same questions being asked over again about bugs and features etc. So what i have done is divided the old pages into a seperate url so that they can all be referenced if needed but are all pointed to this article for the most up to date versions of the code.
You can view the original tag cloud tutorial here.
For legacy, here is the original v2 of the script here.
Bugs fixed
- setLimit didn't work. (20/02/11)
- Removed words get removed if limit applied. (20/02/11)
- Bracket conversion of percentage adjusted to show smaller sizes on less frequent words. (20/02/11)
