<?php
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);
}
$words = ['una' => 1, 'dos' => 2, 'qweqwe' => 3];
var_dump(word_cloud($words));
- Output for 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.27, 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
- array(2) {
[0]=>
string(151) "<div style="width: 400px"><span style="font-size: 48px; color: #0000aa;">dos</span> <span style="font-size: 96px; color: #0000ff;">qweqwe</span> </div>"
[1]=>
int(2)
}
- Output for 5.2.3 - 5.2.17, 5.3.0 - 5.3.29
- Parse error: syntax error, unexpected '[' in /in/mE0VP on line 48
Process exited with code 255. - Output for 4.4.2 - 4.4.9, 5.1.0 - 5.1.6, 5.2.0 - 5.2.2
- <br />
<b>Parse error</b>: syntax error, unexpected '[' in <b>/in/mE0VP</b> on line <b>48</b><br />
Process exited with code 255. - Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1, 5.0.0 - 5.0.5
- <br />
<b>Parse error</b>: parse error, unexpected '[' in <b>/in/mE0VP</b> on line <b>48</b><br />
Process exited with code 255. - Output for 4.3.2 - 4.3.4
- <br />
<b>Parse error</b>: parse error in <b>/in/mE0VP</b> on line <b>48</b><br />
Process exited with code 255.
preferences:
334.25 ms | 401 KiB | 456 Q