以下代码,实现一个标签云的效果。
只给出核心代码,供大家学习参考。
1,创建标签云的函数
{
//I pass through an array of tags
$i=0;
foreach($tags as $tag)
{
$id = $tag['id']; //the tag id, passed through
$name = $tag['tag']; //the tag name, also passed through in the array
//using the mysql count command to sum up the tutorials tagged with that id
$sql = "SELECT COUNT(*) AS totalnum FROM tutorials WHERE tags LIKE '%".$id."%' AND published = 1";
//create the resultset and return it
$res = mysql_query($sql);
$res = mysql_fetch_assoc($res);
//check there are results ;)
if($res)
{
//build an output array, with the tag-name and the number of results
$output[$i]['tag'] = $name;
$output[$i]['num'] = $res['totalnum'];
}
$i++;
}
/*this is just calling another function that does a similar SQL statement, but returns how many pieces of content I have*/
$total_tuts = $this->getNumberOfTutorials();
//ugh, XHTML in PHP? Slap my hands - this isn't best practice, but I was obviously feeling lazy
$html = '<ul >';
//iterate through each item in the $output array (created above)
foreach($output as $tag)
{
//get the number-of-tag-occurances as a percentage of the overall number
$ratio = (100 / $total_tuts) * $tag['num'];
//round the number to the nearest 10
$ratio = round($ratio,-1);
/*append that classname onto the list-item, so if the result was 20%, it comes out as cloud-20*/
$html.= '<li .$ratio.'"><a href="/tag/index.html'.$tag['tag'].'">'.$tag['tag'].'</a></li>';
}
//close the UL
$html.= '</ul>';
return $html;
}
2,css代码部分
/*删除默认的列表样式,使之成为一个普通的清单列表*/
{
list-style-type:none;
margin:0px;
padding:0px;
}
/*设置li的样式*/
.home-item ul.tagcloud li
{
display:inline !important;
margin-right:15px;
line-height:2em;
}
.home-item ul.tagcloud li a
{
display:inline;
}
/*标签云的效果*/
.home-item ul.tagcloud li.cloud-10 a
{
font-size:110%;
}
.home-item ul.tagcloud li.cloud-20 a
{
font-size:120%;
}
.home-item ul.tagcloud li.cloud-30 a
{
font-size:130%;
}
/*************************************
you get the idea, i'm skipping a few
*************************************/
.home-item ul.tagcloud li.cloud-90 a
{
font-size:190%;
}
.home-item ul.tagcloud li.cloud-100 a
{
font-size:200%;
}
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="#" . $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 . floor($size) . 'px'
. '" 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()即可:
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;
}
以下分享的这段简单代码,借助php的正则表达式来验证email地址的有效性。
有兴趣的朋友,可以研究下。
代码:
<html> <head> <title>php正则验证email地址-www.</title> </head> <body> <?php /** * 检测输入的email地址是否有效 * by www. */ if (isset($_POST['posted'])) { $email = $_POST['email']; $theresults = ereg("^[^@ ]+@[^@ ]+.[^@ .]+$", $email, $trashed); if ($theresults) { $isamatch = "有效的email地址"; } else { $isamatch = "无效的email地址"; } echo "经验证,您输入的Email:是" . $isamatch; } ?> <form action="/blog_article/</php echo $_SERVER[.html'PHP_SELF']; ?>" method="POST"> <input type="hidden" name="posted" value="true"> 请输入要验证的E-Mail地址: <input type="text" name="email" value="name@example.com"> <input type="submit" value="Validate"> </form> </body> </html>