Jump to content

Recommended Posts

Hey guys, i'm struggling to develop a form where the users submits text and it must then be displayed as a cloud .. It is based upon the premise that metadata, in the form of tags, is

displayed in various font sizes and weights based upon their importance. The more

important a tag, the bigger it is displayed.

 

How will it work?

 

Your user must be presented with a form containing a single text area where he/she can

enter as much text as they like. They do, however, have to enter a minimum of 100 words. If

they fail to do so they should be advised and enabled to correct it.

To construct the tag cloud you will have to follow through a series of steps:

1. The first is to clean the text. Remove all punctuation from it and convert everything

to lower case. Also remove all stop words. These are words like the, and, you etc.,

 

2. Now you’ll have to count the number of occurrences of each word in the text.

3. Once you know how many times the words appear in the text you can construct your

cloud.

How to construct the cloud

It is important that you use your own creativity and originality when constructing your tag

cloud. Nevertheless, there are certain guidelines which you should follow. Your tag cloud

will always consist of 20 tags which must be sorted alphabetically. If the user entered 150

words, you have to select the 20 words which occurred most and display these in your

cloud. You have complete freedom with regards to the style of your cloud (colours, font

types, font sizes) but you must implement a clearly visible visual hierarchy to distinguish

between more and less important terms.

User Options

When the tag cloud is displayed to the user, you should provide him with the option to

update the tag cloud by adding additional text to it. It is important, therefore, that you store

the original text entered by the user in a session variable and combine it with any new text

he enters. The new cloud should be constructed using both the original and the new text.

Alternatively the user should have the option to create a completely new tag cloud.

 

 

 

Here is my code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<title>Tag Cloud Constructor</title>
</head>
<body bgcolor="black">
<center>
<br/><br/>
<table border="0">
<tr>
<td background='bike.jpg' width='710' height='490' valign='top' style="text-align:left;color:white;font-family:tahoma;font-size:12;border:1 solid white;padding:20px;">


<span style="font-size:35">Tag Cloud Constructor</span><br/><br/><br/>

<?php 

if(isset($_POST['submitted']))

if(empty($_POST['text']))
{
print "<span style='color:red;font-weight:bold;'>Please enter some text!!</span><p/>";
include('form.php');

}

else
{
$cloud_one=$_POST['text'];
$cloud_one=strtolower(stripslashes($cloud_one));//hier convert ek dit na lowercase
$cloud_one=explode(' ',$cloud_one);//hier convert ek die text in array elements

$cloud_one=str_replace(".","",$cloud_one);//hier verwyder ek alle punctuation, ek weet nie presies hoe om die stop words te verwyder nie
$cloud_one=str_replace("'","",$cloud_one);
$cloud_one=str_replace(":","",$cloud_one);
$cloud_one=str_replace(";","",$cloud_one);
$cloud_one=str_replace(",","",$cloud_one);
$cloud_one=str_replace("!","",$cloud_one);
$cloud_one=str_replace("?","",$cloud_one);
$cloud_one=str_replace("_","",$cloud_one);

$cloud_two=array_unique($cloud_one);//verwyder alle duplicate values hier, weet nie of dit nodig is nie
sort($cloud_two);//sorteer die array hier

$count_words =  array();//hier count ek die words in die array,nie seker of ek dit korrek code nie..eks heeltemal weg by die ontwerp van die cloud en die 'tags'
      
      foreach($cloud_one as $a){

         if (array_key_exists($a,  $count_words)){

             $count_words[$a]++;
         }else{

             $count_words[$a]=1;
         }
}

}

else
{
include('form.php');

} 

?>
</body>
</html>	

 

form.php

 

<form name='cloud_constructor' method='post' action='index.php'>

<table cellspacing='10px'>
<tr><td style="vertical-align:top;color:white;font-family:tahoma;font-size:12;">Please enter some text and click submit to create a cloud: </td><td><textarea name='text' rows='10' cols='30'></textarea>

</table>
<input type="hidden" name="submitted" value="TRUE"/>
<input type='submit' value='Submit'/>
</form>

 

The thing is i do not know how to create the tags and how to use session variables..i would appreciate your help..

Link to comment
https://forums.phpfreaks.com/topic/158594-solved-tag-cloud-constructor/
Share on other sites

The first thing is removing the punctuation. You could be forever adding new lines to str_replace for punctuation. You are better removing everything that is not alphabetic in one fowl swoop.

 

Also heres how to count word occurances better

<?php

$text = "This is a sample sentence. It's a fine day. He said, 'this sample looks fantastic'.";
// change to lowercase
$text = strtolower($text);
// remove punctuation
$text = ereg_replace('[^a-zA-Z ]+', '', $text);
// remove stop words
$stopWords = array('a', 'is', 'he');
$text = preg_replace('/\b('.implode("|",$stopWords).')\b/i', '', $text);

// remove any blank spaces
$words = explode(" ", $text);
foreach($words as $key => $word) {
	if(!strlen($word)) {
		unset($words[$key]);
	}
}

// count occurances
$wordCount = array_count_values($words);

print "<pre>";
	print_r($wordCount);
print "</pre>";

?>

I just worked something out for you..

 

<?php
// BY Russell Crevatas
// CONTACT email/msn RussellonMSN@hotmail.com
class TagCloud {
	private $tag_count = array();
	private $tag_formatted = array();
	private $min_size = 10;
	private $max_size = 40;
	public function __construct($tags) {
		foreach ($tags as $v) {
			if ($this->tag_count[strtolower($v)]) $this->tag_count[strtolower($v)]++;
			else $this->tag_count[strtolower($v)] = 1;
		}
		$this->format_each(count($tags));
	}
	private function format_each($count) {
		foreach ($this->tag_count as $k => $v) {
			$m = ($v / $count);
			$s = ceil((($this->max_size - $this->min_size) * $m) + $this->min_size);
			$this->tag_formatted[] = "<span style='font-size: {$s}px; text-transform: uppercase;'>{$k}</span>";
		}
	}
	public function get_cloud() {
		return implode(' ',$this->tag_formatted);
	}
}
$tags = new TagCloud(array('omg','lolwtf','omg','ffs','wtf','owned','ffs','omg','lmao','ffs','omg','rofl','omg','omg','omg'));
echo $tags->get_cloud();
?>

Well, this does it for a string.

 

<?php

$numberofwords = count(explode(" ",$text));

?>

 

And a more advanced version:

 

<?php

function adv_count_words($str){
     $words = 0;
     $str = eregi_replace(" +", " ", $str);
     $array = explode(" ", $str);
     for($i=0;$i < count($array);$i++)
	 {
         if (eregi("[0-9A-Za-zÀ-ÖØ-öø-ÿ]", $array[$i])) 
             $words++;
     }
     return $words;
}

?>

 

Explained here: http://www.reconn.us/content/view/33/48/

Thanx mattal, i used the code, but the thing is it only counts the words from a string..

 

when a user enters say for instance 85 words in a textbox i want it to display a warning message indicating that he/she should atleast enter 100 words..i don't want to count the words of string

Thanx neil helps a lot, slight problem

 

<?php 

if(isset($_POST['submitted']))
{

if(empty($_POST['text'])) OR ($_POST['text'])) <100
{
print "<span style='color:red;font-weight:bold;'>Please enter some text!!</span><p/>";
include('form.php');

}

else
{
$cloud_one=$_POST['text'];
$cloud_one=strtolower(stripslashes($cloud_one));
$cloud_one=count(explode(' ',$cloud_one));

if ($cloud_one<100){
print"You must enter atleast 100 words!";

}
else {print" Thanks for entering the required amount of words";

}

$cloud_one=str_replace('[^a-zA-Z ]+', '', $cloud_one);
$stopwords=array("a", "about", "above", "above", "across", "after", "afterwards", "again", "against", "all", "almost", "alone", "along", "already", "also","although","always","am","among", "amongst", "amoungst", "amount", "an", "and", "another", "any","anyhow","anyone","anything","anyway", "anywhere", "are", "around", "as", "at", "back","be","became", "because","become","becomes", "becoming", "been", "before", "beforehand", "behind", "being", "below", "beside", "besides", "between", "beyond", "bill", "both", "bottom","but", "by", "call", "can", "cannot", "cant", "co", "con", "could", "couldnt", "cry", "de", "describe", "detail", "do", "done", "down", "due", "during", "each", "eg", "eight", "either", "eleven","else", "elsewhere", "empty", "enough", "etc", "even", "ever", "every", "everyone", "everything", "everywhere", "except", "few", "fifteen", "fify", "fill", "find", "fire", "first", "five", "for", "former", "formerly", "forty", "found", "four", "from", "front", "full", "further", "get", "give", "go", "had", "has", "hasnt", "have", "he", "hence", "her", "here", "hereafter", "hereby", "herein", "hereupon", "hers", "herself", "him", "himself", "his", "how", "however", "hundred", "ie", "if", "in", "inc", "indeed", "interest", "into", "is", "it", "its", "itself", "keep", "last", "latter", "latterly", "least", "less", "ltd", "made", "many", "may", "me", "meanwhile", "might", "mill", "mine", "more", "moreover", "most", "mostly", "move", "much", "must", "my", "myself", "name", "namely", "neither", "never", "nevertheless", "next", "nine", "no", "nobody", "none", "noone", "nor", "not", "nothing", "now", "nowhere", "of", "off", "often", "on", "once", "one", "only", "onto", "or", "other", "others", "otherwise", "our", "ours", "ourselves", "out", "over", "own","part", "per", "perhaps", "please", "put", "rather", "re", "same", "see", "seem", "seemed", "seeming", "seems", "serious", "several", "she", "should", "show", "side", "since", "sincere", "six", "sixty", "so", "some", "somehow", "someone", "something", "sometime", "sometimes", "somewhere", "still", "such", "system", "take", "ten", "than", "that", "the", "their", "them", "themselves", "then", "thence", "there", "thereafter", "thereby", "therefore", "therein", "thereupon", "these", "they", "thickv", "thin", "third", "this", "those", "though", "three", "through", "throughout", "thru", "thus", "to", "together", "too", "top", "toward", "towards", "twelve", "twenty", "two", "un", "under", "until", "up", "upon", "us", "very", "via", "was", "we", "well", "were", "what", "whatever", "when", "whence", "whenever", "where", "whereafter", "whereas", "whereby", "wherein", "whereupon", "wherever", "whether", "which", "while", "whither", "who", "whoever", "whole", "whom", "whose", "why", "will", "with", "within", "without", "would", "yet", "you", "your", "yours", "yourself", "yourselves", "the"); 
$cloud_one=preg_replace('/\b('.implode("|",$stopwords).')\b/i', '', $cloud_one);


$wordCount = array_count_values($cloud_one);

print "<pre>";
	print_r($wordCount);
print "</pre>";


}
}

else
{
include('form.php');

}

 

The count function clashes with the array_count_values function..how do i fix this.

Thats because you are storing the word count in the $cloud_one variable when that should be the array of words. Use another variable

 

$cloud_one = explode(' ',$cloud_one);
$cloud_one_word_count = count($cloud_one);
if($cloud_one_word_count < 100) {

}

The code to count the array values is not working.. i'm getting this error message "Warning: array_count_values() [function.array-count-values]: The argument should be an array in C:\wamp\www\Project\index.php on line 55"

 

here's the code again:

<?php 

if(isset($_POST['submitted']))
{

if(empty($_POST['text']))
{
print "<span style='color:red;font-weight:bold;'>Please enter some text!!</span><p/>";
include('form.php');

}

else
{
$cloud_one=$_POST['text'];
$cloud_one=strtolower(stripslashes($cloud_one));
$cloud_one=str_replace('[^a-zA-Z ]+', '', $cloud_one);
$stopwords=array("a", "about", "above", "above", "across", "after", "afterwards", "again", "against", "all", "almost", "alone", "along", "already", "also","although","always","am","among", "amongst", "amoungst", "amount", "an", "and", "another", "any","anyhow","anyone","anything","anyway", "anywhere", "are", "around", "as", "at", "back","be","became", "because","become","becomes", "becoming", "been", "before", "beforehand", "behind", "being", "below", "beside", "besides", "between", "beyond", "bill", "both", "bottom","but", "by", "call", "can", "cannot", "cant", "co", "con", "could", "couldnt", "cry", "de", "describe", "detail", "do", "done", "down", "due", "during", "each", "eg", "eight", "either", "eleven","else", "elsewhere", "empty", "enough", "etc", "even", "ever", "every", "everyone", "everything", "everywhere", "except", "few", "fifteen", "fify", "fill", "find", "fire", "first", "five", "for", "former", "formerly", "forty", "found", "four", "from", "front", "full", "further", "get", "give", "go", "had", "has", "hasnt", "have", "he", "hence", "her", "here", "hereafter", "hereby", "herein", "hereupon", "hers", "herself", "him", "himself", "his", "how", "however", "hundred", "ie", "if", "in", "inc", "indeed", "interest", "into", "is", "it", "its", "itself", "keep", "last", "latter", "latterly", "least", "less", "ltd", "made", "many", "may", "me", "meanwhile", "might", "mill", "mine", "more", "moreover", "most", "mostly", "move", "much", "must", "my", "myself", "name", "namely", "neither", "never", "nevertheless", "next", "nine", "no", "nobody", "none", "noone", "nor", "not", "nothing", "now", "nowhere", "of", "off", "often", "on", "once", "one", "only", "onto", "or", "other", "others", "otherwise", "our", "ours", "ourselves", "out", "over", "own","part", "per", "perhaps", "please", "put", "rather", "re", "same", "see", "seem", "seemed", "seeming", "seems", "serious", "several", "she", "should", "show", "side", "since", "sincere", "six", "sixty", "so", "some", "somehow", "someone", "something", "sometime", "sometimes", "somewhere", "still", "such", "system", "take", "ten", "than", "that", "the", "their", "them", "themselves", "then", "thence", "there", "thereafter", "thereby", "therefore", "therein", "thereupon", "these", "they", "thickv", "thin", "third", "this", "those", "though", "three", "through", "throughout", "thru", "thus", "to", "together", "too", "top", "toward", "towards", "twelve", "twenty", "two", "un", "under", "until", "up", "upon", "us", "very", "via", "was", "we", "well", "were", "what", "whatever", "when", "whence", "whenever", "where", "whereafter", "whereas", "whereby", "wherein", "whereupon", "wherever", "whether", "which", "while", "whither", "who", "whoever", "whole", "whom", "whose", "why", "will", "with", "within", "without", "would", "yet", "you", "your", "yours", "yourself", "yourselves", "the"); 
$cloud_one=preg_replace('/\b('.implode("|",$stopwords).')\b/i', '', $cloud_one);
$cloud_one=explode(' ',$cloud_one);
$cloud_two=count($cloud_one);

if ($cloud_two<100)
{
print"<span style='color:red;font-weight:bold;'> You must enter atleast 100 words!</span><p/>";
include('form.php');

}

else {

print"Thanks for entering the required amount of words";

}
}
$count_words=array_count_values($cloud_one);

}


else
{
include('form.php');

}

?>

 

I need to know how many times the word appears in the text so that i can start constructing my cloud

Firstly your function str_replace() is incorrect as it does not accept regex patterns. Check my post above.

 

$cloud_one =str_replace('[^a-zA-Z ]+', '', $cloud_one);

Should be

$cloud_one = ereg_replace('[^a-zA-Z ]+', '', $cloud_one);

 

Also you will have to get used to not keep using the same variable name where you may need to reuse the results of a previous function on that variable

 

// explode all words into an array
$cloud_one_words = explode(' ',$cloud_one);
// create an array of word occurances
$count_words = array_count_values($cloud_one_words);

 

Also try to make your variable names have something to do with what the variable contains rather than just $cloud_one this makes it easier to follow. i.e. text: $cloud_one_text, words: $cloud_one_words, number of words: $cloud_one_word_count, word occurances: $cloud_one_word_occurances

I changed my code now:

<?php 

if(isset($_POST['submitted']))
{

if(empty($_POST['text']))
{
print "<span style='color:red;font-weight:bold;'>Please enter some text!!</span><p/>";
include('form.php');

}

else
{
$cloud_one=$_POST['text'];
$cloud_one=strtolower(stripslashes($cloud_one));
$cloud_one=ereg_replace('[^a-zA-Z ]+', '', $cloud_one);
$stopwords=array("a", "about", "above", "above", "across", "after", "afterwards", "again", "against", "all", "almost", "alone", "along", "already", "also","although","always","am","among", "amongst", "amoungst", "amount", "an", "and", "another", "any","anyhow","anyone","anything","anyway", "anywhere", "are", "around", "as", "at", "back","be","became", "because","become","becomes", "becoming", "been", "before", "beforehand", "behind", "being", "below", "beside", "besides", "between", "beyond", "bill", "both", "bottom","but", "by", "call", "can", "cannot", "cant", "co", "con", "could", "couldnt", "cry", "de", "describe", "detail", "do", "done", "down", "due", "during", "each", "eg", "eight", "either", "eleven","else", "elsewhere", "empty", "enough", "etc", "even", "ever", "every", "everyone", "everything", "everywhere", "except", "few", "fifteen", "fify", "fill", "find", "fire", "first", "five", "for", "former", "formerly", "forty", "found", "four", "from", "front", "full", "further", "get", "give", "go", "had", "has", "hasnt", "have", "he", "hence", "her", "here", "hereafter", "hereby", "herein", "hereupon", "hers", "herself", "him", "himself", "his", "how", "however", "hundred", "ie", "if", "in", "inc", "indeed", "interest", "into", "is", "it", "its", "itself", "keep", "last", "latter", "latterly", "least", "less", "ltd", "made", "many", "may", "me", "meanwhile", "might", "mill", "mine", "more", "moreover", "most", "mostly", "move", "much", "must", "my", "myself", "name", "namely", "neither", "never", "nevertheless", "next", "nine", "no", "nobody", "none", "noone", "nor", "not", "nothing", "now", "nowhere", "of", "off", "often", "on", "once", "one", "only", "onto", "or", "other", "others", "otherwise", "our", "ours", "ourselves", "out", "over", "own","part", "per", "perhaps", "please", "put", "rather", "re", "same", "see", "seem", "seemed", "seeming", "seems", "serious", "several", "she", "should", "show", "side", "since", "sincere", "six", "sixty", "so", "some", "somehow", "someone", "something", "sometime", "sometimes", "somewhere", "still", "such", "system", "take", "ten", "than", "that", "the", "their", "them", "themselves", "then", "thence", "there", "thereafter", "thereby", "therefore", "therein", "thereupon", "these", "they", "thickv", "thin", "third", "this", "those", "though", "three", "through", "throughout", "thru", "thus", "to", "together", "too", "top", "toward", "towards", "twelve", "twenty", "two", "un", "under", "until", "up", "upon", "us", "very", "via", "was", "we", "well", "were", "what", "whatever", "when", "whence", "whenever", "where", "whereafter", "whereas", "whereby", "wherein", "whereupon", "wherever", "whether", "which", "while", "whither", "who", "whoever", "whole", "whom", "whose", "why", "will", "with", "within", "without", "would", "yet", "you", "your", "yours", "yourself", "yourselves", "the"); 
$cloud_one=preg_replace('/\b('.implode("|",$stopwords).')\b/i', '', $cloud_one);
$cloud_one_words=explode(' ',$cloud_one);
$cloud_two=count($cloud_one);

if ($cloud_two<100)
{
print"<span style='color:red;font-weight:bold;'> You must enter atleast 100 words!</span><p/>";
include('form.php');

}

else {

print"Thanks for entering the required amount of words";

}
}
$count_words=array_count_values($cloud_one_words);

}


else
{
include('form.php');

}

?>

 

but i'm still getting the error message when i click on submit  ???

if i add

$count_words = array_count_values($cloud_one_words);

within the else statement i don't get the error message..am i doing it correct?

 

else {

print"Thanks for entering the required amount of words";
$count_words = array_count_values($cloud_one_words);
}
}

 

Remeber if you change variable names, change in subsequent functions

$cloud_one_words=explode(' ',$cloud_one);
$cloud_two=count($cloud_one);

 

to

$cloud_one_words=explode(' ',$cloud_one);
$cloud_two=count($cloud_one_words);

 

Also check your conditional braces! I have rewritten this for you and tested. I have used cleaner functions for condition testing.

You may also want to think about loading the stop words in from a text file making it easier to add to

<?php 

if(isset($_POST['submitted']))	{
if(!strlen(trim($_POST['text']))) {
	print "<span style='color:red;font-weight:bold;'>Please enter some text!!</span><p/>";
	include('form.php');
}
else {
	// format text
	$cloud_one_text = $_POST['text'];
 	$cloud_one_text = strtolower(stripslashes($cloud_one_text));
 	$cloud_one_text = ereg_replace('[^a-zA-Z ]+', '', $cloud_one_text);

	// remove stopwords
	$stopwords = array("a", "about", "above", "above", "across", "after", "afterwards", "again", "against", "all", "almost", "alone", "along", 
					   "already", "also","although","always","am","among", "amongst", "amoungst", "amount", "an", "and", "another", "any","anyhow","anyone","anything","anyway", 
					   "anywhere", "are", "around", "as", "at", "back","be","became", "because","become","becomes", "becoming", "been", "before", "beforehand", "behind", "being", "below", 
					   "beside", "besides", "between", "beyond", "bill", "both", "bottom","but", "by", "call", "can", "cannot", "cant", "co", "con", "could", "couldnt", "cry", "de", "describe", 
					   "detail", "do", "done", "down", "due", "during", "each", "eg", "eight", "either", "eleven","else", "elsewhere", "empty", 
					   "enough", "etc", "even", "ever", "every", "everyone", "everything", 
					   "everywhere", "except", "few", "fifteen", "fify", "fill", "find", "fire", "first", "five", "for", 
					   "former", "formerly", "forty", "found", "four", "from", "front", "full", "further", "get", "give", 
					   "go","had", "has", "hasnt", "have", "he", "hence", "her", "here", "hereafter", "hereby", "herein", 
					   "hereupon", "hers", "herself", "him", "himself", "his", "how", "however", "hundred", "ie", "if", "in", 
					   "inc", "indeed", "interest", "into", "is", "it", "its", "itself", "keep", "last", "latter", 
					   "latterly", "least", "less", "ltd", "made", "many", "may", "me", "meanwhile", "might", "mill", "mine", 
					   "more", "moreover", "most", "mostly", "move", "much", "must", "my", "myself", "name", "namely", 
					   "neither", "never", "nevertheless", "next", "nine", "no", "nobody", "none", "noone", "nor", "not", 
					   "nothing", "now", "nowhere", "of", "off", "often", "on", "once", "one", "only", "onto", "or", "other", 
					   "others", "otherwise", "our", "ours", "ourselves", "out", "over", "own","part", "per", "perhaps", 
					   "please", "put", "rather", "re", "same", "see", "seem", "seemed", "seeming", "seems", "serious", 
					   "several", "she", "should", "show", "side", "since", "sincere", "six", "sixty", "so", "some", "somehow", "someone", 
					   "something", "sometime", "sometimes", "somewhere", "still", "such", "system", "take", "ten", "than", "that", 
					   "the", "their", "them", "themselves", "then", "thence", "there", "thereafter", "thereby", "therefore", "therein", 
					   "thereupon", "these", "they", "thickv", "thin", "third", "this", "those", "though", "three", "through", "throughout", 
					   "thru", "thus", "to", "together", "too", "top", "toward", "towards", "twelve", "twenty", "two", "un", "under", "until", 
					   "up", "upon", "us", "very", "via", "was", "we", "well", "were", "what", "whatever", "when", "whence", "whenever", "where", 
					   "whereafter", "whereas", "whereby", "wherein", "whereupon", "wherever", "whether", "which", "while", "whither", 
					   "who", "whoever", "whole", "whom", "whose", "why", "will", "with", "within", "without", "would", "yet", "you", 
					   "your", "yours", "yourself", "yourselves", "the"); 

	$cloud_one_text = preg_replace('/\b('.implode("|",$stopwords).')\b/i', '', $cloud_one_text);
	// extract words
		$cloud_one_words = explode(' ',$cloud_one_text);
		$cloud_one_word_count = count($cloud_one_words);

	if($cloud_one_word_count < 100) {
		print"<span style='color:red;font-weight:bold;'> You must enter atleast 100 words!</span><p/>";
		include('form.php');
	}
	else {
		print "Thanks for entering the required amount of words";
		// remove empty words
		foreach($cloud_one_words as $key => $word) {
			if(!strlen($word)) {
				unset($cloud_one_words[$key]);
			}
		}


		$cloud_one_word_occurances = array_count_values($cloud_one_words);

		print "<pre>";
			print_r($cloud_one_word_occurances);
		print "</pre>";
		exit();
	}
}
}
else {
include('form.php');
}

?>

Can you guys help me out with this..the code above didn't work

 

Your tag cloud will always consist of 20 tags which must be sorted alphabetically. If the user entered 150

words, you have to select the 20 words which occurred most and display these in your

cloud.

 

below is a screenshot of how it should look, or a working example

 

[attachment deleted by admin]

I don't know how to proceed further, i've been on google the whole day going through tutorials and how to's but no help..i got the following code from a site, maybe i can use it , but i'm not sure on how to implement it..i'm really struggling here, been busy coding the whole day..please help

 

function printTagCloud($tags) {
        // $tags is the array
        
        arsort($tags);
        
        $max_size = 32; // max font size in pixels
        $min_size = 12; // min font size in pixels
        
        // largest and smallest array values
        $max_qty = max(array_values($tags));
        $min_qty = min(array_values($tags));
        
        // find the range of values
        $spread = $max_qty - $min_qty;
        if ($spread == 0) { // we don't want to divide by zero
                $spread = 1;
        }
        
        // set the font-size increment
        $step = ($max_size - $min_size) / ($spread);
        
        // loop through the tag array
        foreach ($tags as $key => $value) {
                // calculate font-size
                // find the $value in excess of $min_qty
                // multiply by the font-size increment ($size)
                // and add the $min_size set above
                $size = round($min_size + (($value - $min_qty) * $step));
        
                echo '<a href="#" style="font-size: ' . $size . 'px" title="' . $value . ' things tagged with ' . $key . '">' . $key . '</a> ';
        }
}

$tags = array('weddings' => 32, 'birthdays' => 41, 'landscapes' => 62, 'ham' => 51, 'chicken' => 23, 'food' => 91, 'turkey' => 47, 'windows' => 82, 'apple' => 27);

printTagCloud($tags);

You just do like

 

// put class here

// replace 'the_text_field' with the name of the tags textarea :)

$text = explode(' ',str_replace(array('\'','the','and','or',',','  '),'',$_POST['the_text_field']));

$tags = new TagCloud($text);

echo $tags->get_cloud();

 

Can anyone tell me what's wrong with this? y the tags don't want to display:

 

<?php 

if(isset($_POST['submitted']))
{

if(!strlen(trim($_POST['text']))) 
{
print "<span style='color:red;font-weight:bold;'>Please enter some text!!</span><p/>";
include('form.php');

}

else {

//format text

$cloud_one_text=$_POST['text'];
$cloud_one_text=strtolower(stripslashes($cloud_one_text));
$cloud_one_text=str_replace(".","",$cloud_one_text);
$cloud_one_text=str_replace("'","",$cloud_one_text);
$cloud_one_text=str_replace(":","",$cloud_one_text);
$cloud_one_text=str_replace(";","",$cloud_one_text);
$cloud_one_text=str_replace(",","",$cloud_one_text);
$cloud_one_text=str_replace("!","",$cloud_one_text);
$cloud_one_text=str_replace("?","",$cloud_one_text);
$cloud_one_text=str_replace("_","",$cloud_one_text);


//remove stopwords

$stopwords=array("a", "about", "above", "above", "across", "after", "afterwards", "again", "against", "all", "almost", "alone", "along", "already", "also","although","always","am","among", "amongst", "amoungst", "amount", "an", "and", "another", "any","anyhow","anyone","anything","anyway", "anywhere", "are", "around", "as", "at", "back","be","became", "because","become","becomes", "becoming", "been", "before", "beforehand", "behind", "being", "below", "beside", "besides", "between", "beyond", "bill", "both", "bottom","but", "by", "call", "can", "cannot", "cant", "co", "con", "could", "couldnt", "cry", "de", "describe", "detail", "do", "done", "down", "due", "during", "each", "eg", "eight", "either", "eleven","else", "elsewhere", "empty", "enough", "etc", "even", "ever", "every", "everyone", "everything", "everywhere", "except", "few", "fifteen", "fify", "fill", "find", "fire", "first", "five", "for", "former", "formerly", "forty", "found", "four", "from", "front", "full", "further", "get", "give", "go", "had", "has", "hasnt", "have", "he", "hence", "her", "here", "hereafter", "hereby", "herein", "hereupon", "hers", "herself", "him", "himself", "his", "how", "however", "hundred", "ie", "if", "in", "inc", "indeed", "interest", "into", "is", "it", "its", "itself", "keep", "last", "latter", "latterly", "least", "less", "ltd", "made", "many", "may", "me", "meanwhile", "might", "mill", "mine", "more", "moreover", "most", "mostly", "move", "much", "must", "my", "myself", "name", "namely", "neither", "never", "nevertheless", "next", "nine", "no", "nobody", "none", "noone", "nor", "not", "nothing", "now", "nowhere", "of", "off", "often", "on", "once", "one", "only", "onto", "or", "other", "others", "otherwise", "our", "ours", "ourselves", "out", "over", "own","part", "per", "perhaps", "please", "put", "rather", "re", "same", "see", "seem", "seemed", "seeming", "seems", "serious", "several", "she", "should", "show", "side", "since", "sincere", "six", "sixty", "so", "some", "somehow", "someone", "something", "sometime", "sometimes", "somewhere", "still", "such", "system", "take", "ten", "than", "that", "the", "their", "them", "themselves", "then", "thence", "there", "thereafter", "thereby", "therefore", "therein", "thereupon", "these", "they", "thickv", "thin", "third", "this", "those", "though", "three", "through", "throughout", "thru", "thus", "to", "together", "too", "top", "toward", "towards", "twelve", "twenty", "two", "un", "under", "until", "up", "upon", "us", "very", "via", "was", "we", "well", "were", "what", "whatever", "when", "whence", "whenever", "where", "whereafter", "whereas", "whereby", "wherein", "whereupon", "wherever", "whether", "which", "while", "whither", "who", "whoever", "whole", "whom", "whose", "why", "will", "with", "within", "without", "would", "yet", "you", "your", "yours", "yourself", "yourselves", "the"); 
$cloud_one_text=preg_replace('/\b('.implode("|",$stopwords).')\b/i', '', $cloud_one_text);

//extract words

$cloud_one_words=explode(' ',$cloud_one_text);
$cloud_one_word_count=count($cloud_one_words);

if ($cloud_one_word_count<100)
{
print"<span style='color:red;font-weight:bold;'> You must enter atleast 100 words!</span><p/>";
include('form.php');

}

else {

print"Thanks for entering the required amount of words<br/><br/>";
$count_words = array_count_values($cloud_one_words);

$tagnumber=120;
$min_font = "10"; 
$max_font = "30";
$tags= $_POST['text'];
$numtags = count ($tags);
sort ($tags);
$increment = intval($numtags/($max_font-$min_font));
$size = $min_font ;
for ($i=0; $i < $numtags; $i++) {
$output[$tags[$i][_content]] = $size ;
if ($increment == 0 || $i % $increment == 0 ) 
$size++;
}
}
ksort($output);
foreach($output as $tg => $sz){
echo " <a href='".$url.$tg."' style='font-size: ".$sz."px;'>".$tg."</a> \n" ;
    }


}
}
else
{
include('form.php');

}

?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.