教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 php 云标签的简单范例

php 云标签的简单范例

发布时间:2016-10-28   编辑:jiaochengji.com
本文介绍下,php实现云标签的一个简单例子,老外写的,很简炼。有需要的朋友,可以参考下人家的写法。

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

<?php
/**
* @php 云标签
* @param $tags 标签列表
* @edit:www.jbxue.com
*/
function printTagCloud($tags) {
  // $tags is the array
  
  arsort($tags);
  
  $max_size = 32; // max font size in pixels
  $min_size = 12; // min font size in pixels
  
  // largest and smallest array values
  $max_qty = max(array_values($tags));
  $min_qty = min(array_values($tags));
  
  // find the range of values
  $spread = $max_qty - $min_qty;
  if ($spread == 0) { // we don't want to divide by zero
    $spread = 1;
  }
  
  // set the font-size increment
  $step = ($max_size - $min_size) / ($spread);
  
  // loop through the tag array
  foreach ($tags as $key => $value) {
    // calculate font-size
    // find the $value in excess of $min_qty
    // multiply by the font-size increment ($size)
    // and add the $min_size set above
    $size = round($min_size + (($value - $min_qty) * $step));
  
    echo '<a href="#" style="font-size: ' . $size . 'px" title="' . $value . ' things tagged with ' . $key . '">' . $key . '</a> ';
  }
}

//调用示例
$tags = array('weddings' => 32, 'birthdays' => 41, 'landscapes' => 62, 'ham' => 51, 'chicken' => 23, 'food' => 91, 'turkey' => 47,
 'windows' => 82, 'apple' => 27);
printTagCloud($tags);
?>

您可能感兴趣的文章:
php 云标签的简单范例
php 标签云效果(简单示例,入门参考)
Typecho 彩色标签云的实现代码
php 创建标签云的代码示例
php mysql实现标签云的实例代码
php删除字符串中html标签的函数
php写的创建标签云的函数
PHP编码开发规范的介绍(附示例)
php操作xml文件的一些简单范例
PHP删除HTMl标签的代码

[关闭]
~ ~