当前位置:Gxlcms > PHP教程 > php云标签的简单范例

php云标签的简单范例

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

本文介绍下,php实现云标签的一个简单例子,老外写的,很简炼。有需要的朋友,可以参考下人家的写法。

php 云标签的例子,代码如下:

  1. <!--?php
  2. /**
  3. * @PHP 云标签
  4. * @param $tags 标签列表
  5. * @edit:bbs.it-home.org
  6. */
  7. function printTagCloud($tags) {
  8. // $tags is the array
  9. arsort($tags);
  10. $max_size = 32; // max font size in pixels
  11. $min_size = 12; // min font size in pixels
  12. // largest and smallest array values
  13. $max_qty = max(array_values($tags));
  14. $min_qty = min(array_values($tags));
  15. // find the range of values
  16. $spread = $max_qty - $min_qty;
  17. if ($spread == 0) { // we don't want to divide by zero
  18. $spread = 1;
  19. }
  20. // set the font-size increment
  21. $step = ($max_size - $min_size) / ($spread);
  22. // loop through the tag array
  23. foreach ($tags as $key =--> $value) {
  24. // calculate font-size
  25. // find the $value in excess of $min_qty
  26. // multiply by the font-size increment ($size)
  27. // and add the $min_size set above
  28. $size = round($min_size + (($value - $min_qty) * $step));
  29. echo '' . $key . ' ';
  30. }
  31. }
  32. //调用示例
  33. $tags = array('weddings' => 32, 'birthdays' => 41, 'landscapes' => 62, 'ham' => 51, 'chicken' => 23, 'food' => 91, 'turkey' => 47,
  34. 'windows' => 82, 'apple' => 27);
  35. printTagCloud($tags);
  36. ?>

人气教程排行