|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | <? /* @wordCloud Author: Derek Harvey Website: www.lotsofcode.com @Description PHP Tag Cloud Class, a nice and simple way to create a php tag cloud, a database and non-database solution. */ class wordCloud { var $wordsArray = array(); var $refsArray = array(); /* * PHP 5 Constructor * * @param array $words * @return void */ function __construct($words = false) { if ($words !== false && is_array($words)) { foreach ($words as $key => $value) { $this->addWord($value); } } } /* * PHP 4 Constructor * * @param array $words * @return void */ function wordCloud($words = false) { $this->__construct($words); } /* * Assign word to array * * @param string $word * @return string */ function addWord($word, $value = 1, $ref = 0) { if (array_key_exists($word, $this->wordsArray)) $this->wordsArray[$word] += $value; else $this->wordsArray[$word] = $value; $this->refsArray[$word] = $ref; return $this->wordsArray[$word]; } /* * Shuffle associated names in array */ function shuffleCloud() { $keys = array_keys($this->wordsArray); shuffle($keys); if (count($keys) && is_array($keys)) { $tmpArray = $this->wordsArray; $this->wordsArray = array(); foreach ($keys as $key => $value) $this->wordsArray[$value] = $tmpArray[$value]; } } /* * Calculate size of words array */ function getCloudSize() { return array_sum($this->wordsArray); } /* * Get the class range using a percentage * * @returns int $class */ function getClassFromPercent($percent) { if ($percent >= 99) $class = 1; else if ($percent >= 70) $class = 2; else if ($percent >= 60) $class = 3; else if ($percent >= 50) $class = 4; else if ($percent >= 40) $class = 5; else if ($percent >= 30) $class = 6; else if ($percent >= 20) $class = 7; else if ($percent >= 10) $class = 8; else if ($percent >= 5) $class = 9; else $class = 0; return $class; } /* * Create the HTML code for each word and apply font size. * * @returns string $spans */ function showCloud($returnType = "html") { $this->shuffleCloud(); $this->max = max($this->wordsArray); if (is_array($this->wordsArray)) { $return = ($returnType == "html" ? "" : ($returnType == "array" ? array() : "")); foreach ($this->wordsArray as $word => $popularity) { $sizeRange = $this->getClassFromPercent(($popularity / $this->max) * 100); if ($returnType == "array") { $return[$word]['word'] = $word; $return[$word]['ref'] = $this->refsArray[$word]; $return[$word]['sizeRange'] = $sizeRange; if ($currentColour) $return[$word]['randomColour'] = $currentColour; } else if ($returnType == "html") { $return .= "<span class='word size{$sizeRange}'> {$word} </span>"; } } return $return; } } } ?> |