当前位置:Gxlcms > PHP教程 > php创建标签云的代码示例

php创建标签云的代码示例

时间:2021-07-01 10:21:17 帮助过:18人阅读

本文介绍下,用php创建标签云的一段代码,通过自定义的php函数创建标签云效果。有需要的朋友参考下。

php 标签云的创建代码一例,如下:

  1. <!--?php
  2. /**
  3. * php 标签云效果
  4. * edit: bbs.it-home.org
  5. */
  6. function getCloud( $data = array(), $minFontSize = 12, $maxFontSize = 30 )
  7. {
  8. $minimumCount = min( array_values( $data ) );
  9. $maximumCount = max( array_values( $data ) );
  10. $spread = $maximumCount - $minimumCount;
  11. $cloudHTML = '';
  12. $cloudTags = array();
  13. $spread == 0 && $spread = 1;
  14. foreach( $data as $tag =--> $count )
  15. {
  16. $size = $minFontSize + ( $count - $minimumCount )
  17. * ( $maxFontSize - $minFontSize ) / $spread;
  18. $cloudTags[] = ''
  19. . htmlspecialchars( stripslashes( $tag ) ) . '';
  20. }
  21. return join( "\n", $cloudTags ) . "\n";
  22. }
  23. /*** Sample usage ***/
  24. $arr = Array('Actionscript' => 35, 'Adobe' => 22, 'Array' => 44, 'Background' => 43,
  25. 'Blur' => 18, 'Canvas' => 33, 'Class' => 15, 'Color Palette' => 11, 'Crop' => 42,
  26. 'Delimiter' => 13, 'Depth' => 34, 'Design' => 8, 'Encode' => 12, 'Encryption' => 30,
  27. 'Extract' => 28, 'Filters' => 42);
  28. echo getCloud($arr, 12, 36);

我收藏的一段php标签云的核心代码:

  1. <!--?php
  2. $artist = array("the roots","michael jackson","billy idol","more","and more","and_YET_MORE");
  3. $count = array(5,3,9,1,1,3);
  4. $highest = max($count);
  5. for ((int) $x = 0; x < count($artist); $x++)
  6. {
  7. $normalized = $count[$x] / $highest;
  8. $heading = ceil($normalized * 6); // 6 heading types
  9. echo "<h".$heading."-->".$artist[$x]."";
  10. }
  11. ?>

人气教程排行