Home > Backend Development > PHP Tutorial > Simple example of php cloud tag

Simple example of php cloud tag

WBOY
Release: 2016-07-25 08:57:03
Original
996 people have browsed it
This article introduces a simple example of implementing cloud tags in PHP. It is written by a foreigner and is very concise. Friends in need can refer to other people's writing methods.

php cloud tag example, the code is as follows:

<?php
/**
* @PHP 云标签
* @param $tags 标签列表
* @edit:bbs.it-home.org
*/
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 '' . $key . ' ';
  }
}

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


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template