PHP Tag Cloud Based on Word Frequency


 / Published in: PHP
 

URL: http://www.scriptplayground.com/tutorials/php/Tag-Cloud/

I was only responsible for wrapping a demo around the function. See URL for original script.

  1. <?php
  2. /**
  3.  * Tag cloud demo based on word frequency
  4.  * @author: unknown
  5.  * @since: 2007-02-27
  6.  */
  7.  
  8. // Store frequency of words in an array
  9. $freqData = array();
  10.  
  11. // Random words
  12. $lorem = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
  13. Phasellus vestibulum ullamcorper tortor. Aenean quis lacus quis neque
  14. adipiscing ultricies. Pellentesque tincidunt ligula vitae nibh ornare
  15. pharetra. Proin dignissim tortor. Donec et ipsum nec tellus gravida
  16. tempor. Aliquam ullamcorper purus vel felis. Praesent faucibus.
  17. Curabitur porta. Nulla in lorem quis mi lacinia fringilla. Integer
  18. adipiscing mi quis felis. Pellentesque habitant morbi tristique senectus
  19. et netus et malesuada fames ac turpis egestas. Quisque sagittis ante in
  20. arcu. Sed libero enim, venenatis sit amet, vestibulum at, porttitor id,
  21. neque. Vestibulum ornare semper erat. Sed tincidunt nibh et massa. Cras
  22. sed diam. Quisque blandit enim.
  23.  
  24.  
  25. Sed nonummy. Aenean mollis turpis quis enim. Nam massa nulla, varius
  26. molestie, aliquet et, feugiat eget, nisi. Sed mollis, leo ut pretium
  27. placerat, nibh turpis egestas ipsum, sed aliquam neque enim in risus.
  28. Nullam nisl. Sed tincidunt leo quis tellus. Mauris non lorem. Aenean
  29. tristique justo at arcu. Fusce et lorem. Nam sodales. Mauris condimentum
  30. diam. Nam commodo. Cum sociis natoque penatibus et magnis dis parturient
  31. montes, nascetur ridiculus mus. Cras ac risus. Proin et dolor laoreet mi
  32. gravida sodales. Duis bibendum, ipsum posuere egestas posuere, dui lacus
  33. feugiat turpis, id tincidunt urna est sit amet est. Cras eu sem.
  34. ";
  35.  
  36. // Get individual words and build a frequency table
  37. foreach( str_word_count( $lorem, 1 ) as $word )
  38. {
  39. // For each word found in the frequency table, increment its value by one
  40. array_key_exists( $word, $freqData ) ? $freqData[ $word ]++ : $freqData[ $word ] = 0;
  41. }
  42.  
  43. // ==============================================================
  44. // = Function to actually generate the cloud from provided data =
  45. // ==============================================================
  46. function getCloud( $data = array(), $minFontSize = 12, $maxFontSize = 30 )
  47. {
  48. $minimumCount = min( array_values( $data ) );
  49. $maximumCount = max( array_values( $data ) );
  50. $spread = $maximumCount - $minimumCount;
  51. $cloudHTML = '';
  52. $cloudTags = array();
  53.  
  54. $spread == 0 && $spread = 1;
  55.  
  56. foreach( $data as $tag => $count )
  57. {
  58. $size = $minFontSize + ( $count - $minimumCount )
  59. * ( $maxFontSize - $minFontSize ) / $spread;
  60. $cloudTags[] = '<a style="font-size: ' . floor( $size ) . 'px'
  61. . '" class="tag_cloud" href="http://www.google.com/search?q=' . $tag
  62. . '" title="\'' . $tag . '\' returned a count of ' . $count . '">'
  63. . htmlspecialchars( stripslashes( $tag ) ) . '</a>';
  64. }
  65.  
  66. return join( "\n", $cloudTags ) . "\n";
  67. }
  68. ?>
  69. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  70. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  71.  
  72. <html xmlns="http://www.w3.org/1999/xhtml">
  73. <head>
  74. <title>Tag Cloud Demo</title>
  75. <style type="text/css" media="screen">
  76. /*<![CDATA[*/
  77. .tag_cloud { padding: 3px; text-decoration: none; }
  78. .tag_cloud:link { color: #81d601; }
  79. .tag_cloud:visited { color: #019c05; }
  80. .tag_cloud:hover { color: #ffffff; background: #69da03; }
  81. .tag_cloud:active { color: #ffffff; background: #ACFC65; }
  82. /*]]>*/
  83. </style>
  84. </head>
  85.  
  86. <body>
  87. <h1>Sample Tag Cloud</h1>
  88. <div id="wrapper">
  89. <?php echo getCloud( $freqData ) ?>
  90. </div>
  91. </body>
  92. </html>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: skywalker on January 15, 2008

Thank you.

You need to login to post a comment.