Create A Tag Cloud


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. function getCloud( $data = array(), $minFontSize = 12, $maxFontSize = 30 )
  2. {
  3. $minimumCount = min( array_values( $data ) );
  4. $maximumCount = max( array_values( $data ) );
  5. $spread = $maximumCount - $minimumCount;
  6. $cloudHTML = '';
  7. $cloudTags = array();
  8.  
  9. $spread == 0 && $spread = 1;
  10.  
  11. foreach( $data as $tag => $count )
  12. {
  13. $size = $minFontSize + ( $count - $minimumCount )
  14. * ( $maxFontSize - $minFontSize ) / $spread;
  15. $cloudTags[] = '<a style="font-size: ' . floor( $size ) . 'px'
  16. . '" class="tag_cloud" href="#" title="\'' . $tag .
  17. '\' returned a count of ' . $count . '">'
  18. . htmlspecialchars( stripslashes( $tag ) ) . '</a>';
  19. }
  20.  
  21. return join( "\n", $cloudTags ) . "\n";
  22. }
  23. /**************************
  24. **** Sample usage ***/
  25. $arr = Array('Actionscript' => 35, 'Adobe' => 22, 'Array' => 44, 'Background' => 43,
  26. 'Blur' => 18, 'Canvas' => 33, 'Class' => 15, 'Color Palette' => 11, 'Crop' => 42,
  27. 'Delimiter' => 13, 'Depth' => 34, 'Design' => 8, 'Encode' => 12, 'Encryption' => 30,
  28. 'Extract' => 28, 'Filters' => 42);
  29. echo getCloud($arr, 12, 36);

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.