分享二个php 标签云的实例代码

发布时间:2020-12-06编辑:脚本学堂
本文介绍下,php标签云的二个实现代码,有需要的朋友参考下。

1,php标签云的效果示例一

复制代码 代码示例:

<?php
//标签云效果
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);
?>

2,php标签云的效果示例二

复制代码 代码示例:

<style type="text/css">
.tag_cloud { padding: 3px; text-decoration: none; }
.tag_cloud:link  { color: #81d601; }
.tag_cloud:visited { color: #019c05; }
.tag_cloud:hover { color: #ffffff; background: #69da03; }
.tag_cloud:active { color: #ffffff; background: #ACFC65; }
</style>
function get_tag_data() {
    $arr = Array('Actionscript' => 35, 'Adobe' => 22, 'Array' => 44, 'Background' => 43,
 'Blur' => 18, 'Canvas' => 33, 'Class' => 15, 'Color Palette' => 11, 'Crop' => 42,
 'Delimiter' => 13, 'Depth' => 34, 'Design' => 8, 'Encode' => 12, 'Encryption' => 30,
 'Extract' => 28, 'Filters' => 42, 'Flash' => 32, 'Functions' => 19,
 'Gaussian Blur' => 44, 'Grafix' => 49, 'Graphics' => 35, 'Hue' => 47, 'Illustrator' => 8,
 'Image Ready' => 12, 'Javascript' => 47, 'Jpeg' => 15,  'Keyboard' => 18, 'Level' => 28,
 'Liquify' => 30, 'Listener' => 10, 'Logo' => 12, 'Loops' => 22, 'Macromedia' => 26,
 'Method' => 28, 'MySQL' => 18, 'Obfuscation' => 13, 'Object' => 39, 'Optimize' => 25,
 'PDF' => 37, 'PHP' => 44, 'PSD' => 17, 'Photography' => 28, 'Photoshop' => 46,
 'Revert' => 50, 'Saturation' => 35, 'Save as' => 28, 'Scope' => 11, 'Scripting' => 9,
 'Security' => 41, 'Sharpen' => 49, 'Switch' => 41, 'Templates' => 11, 'Texture' => 22,
 'Tool Palette' => 30, 'Variables' => 50);
    return $arr;

function get_tag_cloud() {
// Default font sizes
$min_font_size = 12;
$max_font_size = 30;

// Pull in tag data
$tags = get_tag_data();

//构造标签云的html代码
$cloud_html = '';
$cloud_tags = array(); // create an array to hold tag code
foreach ($tags as $tag => $count) {
 $size = $min_font_size + ($count - $minimum_count)
  * ($max_font_size - $min_font_size) / $spread;
 $cloud_tags[] = '<a style="font-size: '. floor($size) . 'px'
  . '" class="tag_cloud" href="http://www.google.com/search?q=' . $tag
  . '" title="'' . $tag  . '' returned a count of ' . $count . '">'
  . htmlspecialchars(stripslashes($tag)) . '</a>';
}
$cloud_html = join("n", $cloud_tags) . "n";
return $cloud_html;
}
Usage:
<h3>php标签云效果</h3>
<div id="wrapper"
<!-- BEGIN Tag Cloud -->
<?php print get_tag_cloud(); ?>
<!-- END Tag Cloud -->
</div>

说明:
如果需要使用mysql数据库来存储与获取标签信息,请将以上函数,替换上面的函数get_tag_data()即可:

复制代码 代码示例:
function get_tag_data() {
  mysql_connect('localhost', 'username', 'password');
  mysql_select_db('database');
  $result = mysql_query("SELECT * FROM tags GROUP BY tag ORDER BY count DESC");
  while($row = mysql_fetch_array($result)) {
    $arr[$row['tag']] = $row['count '];
  }
  ksort($arr);
  return $arr;
}