<?php
/* PHP Word Cloud Generator Example/* --- BEGIN PHP CODE --- */
$stopwords = "able,about,above,according,accordingly,
a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";
$stopwords = explode(",", $stopwords);
/* Builds an array of words with all stop words filtered out. (case-insensitive) */
function filter_stopwords($words, $stopwords) {
foreach ($words as $pos => $word) {
if (!in_array(strtolower($word), $stopwords, TRUE)) {
$filtered_words[$pos] = $word;
}
}
return $filtered_words;
}
/* Builds an array of words as keys and their frequency as the value. (case-insensitive) */
function word_freq($words) {
$frequency_list = array();
foreach ($words as $pos => $word) {
$word = strtolower($word);
if (array_key_exists($word, $frequency_list)) {
++$frequency_list[$word];
}
else {
$frequency_list[$word] = 1;
}
}
return $frequency_list;
}
/* Optional function to filter out words below a specific frequency. */
function freq_filter($words, $filter) {
return array_filter(
$words,
function($v) use($filter) {
if ($v >= $filter) return $v;
}
);
}
/* Builds the word cloud and returns a string containing a div of the word cloud. */
function word_cloud($words, $div_size = 400) {
$tags = 0;
$cloud = "<div style=\"width: {$div_size}px\">";
/* This word cloud generation algorithm was taken from the Wikipedia page on "word cloud"
with some minor modifications to the implementation */
/* Initialize some variables */
$fmax = 96; /* Maximum font size */
$fmin = 8; /* Minimum font size */
$tmin = min($words); /* Frequency lower-bound */
$tmax = max($words); /* Frequency upper-bound */
foreach ($words as $word => $frequency) {
if ($frequency > $tmin) {
$font_size = floor( ( $fmax * ($frequency - $tmin) ) / ( $tmax - $tmin ) );
/* Define a color index based on the frequency of the word */
$r = $g = 0; $b = floor( 255 * ($frequency / $tmax) );
$color = '#' . sprintf('%02s', dechex($r)) . sprintf('%02s', dechex($g)) . sprintf('%02s', dechex($b));
}
else {
$font_size = 0;
}
if ($font_size >= $fmin) {
$cloud .= "<span style=\"font-size: {$font_size}px; color: $color;\">$word</span> ";
$tags++;
}
}
$cloud .= "</div>";
return array($cloud, $tags);
}
if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST' && $_POST['text'] != '') {
/* If the request verb is POST and the appropriate field is supplied and isn't
empty, we will attempt to generate the word cloud. */
$text = $_POST['text']; /* Get the posted text */
$words = str_word_count($text, 1); /* Generate list of words */
$word_count = count($words); /* Word count */
$unique_words = count( array_unique($words) ); /* Unique word count */
$words_filtered = filter_stopwords($words, $stopwords); /* Filter out stop words from the word list */
$word_frequency = word_freq($words_filtered); /* Build a word frequency list */
/* Optionally, you can filter out words below a specific frequency... Uncomment the line below to do so */
/* $word_frequency = freq_filter($word_frequency, 3); */
$word_c = word_cloud($word_frequency); /* Generate a word cloud and get number of tags */
$word_cloud = $word_c[0]; /* The word cloud */
$tags = $word_c[1]; /* The number of tags in the word cloud*/
}
else {
/* Otherwise there is nothing to do... */
$text = "";
$word_count = $unique_words = $tags = 0;
$word_cloud = null;
}
/* --- END PHP CODE --- */
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>PHP Word Cloud Generator Example - By Sherif Ramadan - sheriframadan.com</title>
</head>
<body>
<form method="post" action="">
<textarea cols="100" rows="15" name="text"></textarea>
<br />
<input type="submit" value="Generate Word Cloud" />
</form>
<p><b>Number of words found:</b> <?php echo $word_count; ?></p>
<p><b>Number of unique words:</b> <?php echo $unique_words; ?></p>
<p><b>Number of words tagged:</b> <?php echo $tags; ?></p>
<?php echo $word_cloud; ?>
</body>
</html>