Chinaunix首页 | 论坛 | 博客
  • 博客访问: 730116
  • 博文数量: 204
  • 博客积分: 6552
  • 博客等级: 准将
  • 技术积分: 2724
  • 用 户 组: 普通用户
  • 注册时间: 2007-09-29 18:41
文章分类

全部博文(204)

文章存档

2012年(6)

2011年(66)

2010年(99)

2009年(31)

2008年(2)

我的朋友

分类:

2011-09-04 21:18:08

[代码] 关键词高亮

1 function highlight($sString, $aWords) {
2     if (!is_array ($aWords) || empty ($aWords) || !is_string ($sString)) {
3         return false;
4     }
5   
6     $sWords = implode ('|', $aWords);
7     return preg_replace ('@\b('.$sWords.')\b@si', '$1', $sString);
8 }

[代码] 获取你的Feedburner的用户

01 function get_average_readers($feed_id,$interval = 7){
02     $today = date('Y-m-d', strtotime("now"));
03     $ago = date('Y-m-d', strtotime("-".$interval." days"));
04     $feed_url="".$feed_id."&dates=".$ago.",".$today;
05     $ch = curl_init();
06     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
07     curl_setopt($ch, CURLOPT_URL, $feed_url);
08     $data = curl_exec($ch);
09     curl_close($ch);
10     $xml = new SimpleXMLElement($data);
11     $fb = $xml->feed->entry['circulation'];
12   
13     $nb = 0;
14     foreach($xml->feed->children() as $circ){
15         $nb += $circ['circulation'];
16     }
17   
18     return round($nb/$interval);
19 }

[代码] 自动生成密码

01 function generatePassword($length=9, $strength=0) {
02     $vowels = 'aeuy';
03     $consonants = 'bdghjmnpqrstvz';
04     if ($strength >= 1) {
05         $consonants .= 'BDGHJLMNPQRSTVWXZ';
06     }
07     if ($strength >= 2) {
08         $vowels .= "AEUY";
09     }
10     if ($strength >= 4) {
11         $consonants .= '23456789';
12     }
13     if ($strength >= 8 ) {
14         $vowels .= '@#$%';
15     }
16   
17     $password = '';
18     $alt = time() % 2;
19     for ($i = 0; $i < $length; $i++) {
20         if ($alt == 1) {
21             $password .= $consonants[(rand() % strlen($consonants))];
22             $alt = 0;
23         } else {
24             $password .= $vowels[(rand() % strlen($vowels))];
25             $alt = 1;
26         }
27     }
28     return $password;
29 }

[代码] 压缩多个CSS文件

01 header('Content-type: text/css');
02 ob_start("compress");
03 function compress($buffer) {
04   /* remove comments */
05   $buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
06   /* remove tabs, spaces, newlines, etc. */
07   $buffer = str_replace(array("\r\n", "\r", "\n", "\t", '  ', '    ', '    '), '', $buffer);
08   return $buffer;
09 }
10   
11 /* your css files */
12 include('master.css');
13 include('typography.css');
14 include('grid.css');
15 include('print.css');
16 include('handheld.css');
17   
18 ob_end_flush();

[代码] 获取短网址

1 function getTinyUrl($url) {
2     return file_get_contents("".$url);
3 }

[代码] 根据生日计算年龄

01 function age($date){
02     $year_diff = '';
03     $time = strtotime($date);
04     if(FALSE === $time){
05         return '';
06     }
07   
08     $date = date('Y-m-d', $time);
09     list($year,$month,$day) = explode("-",$date);
10     $year_diff = date("Y") – $year;
11     $month_diff = date("m") – $month;
12     $day_diff = date("d") – $day;
13     if ($day_diff < 0 || $month_diff < 0) $year_diff–;
14   
15     return $year_diff;
16 }

[代码] 计算执行时间

01 //Create a variable for start time
02 $time_start = microtime(true);
03   
04 // Place your PHP/HTML/JavaScript/CSS/Etc. Here
05   
06 //Create a variable for end time
07 $time_end = microtime(true);
08 //Subtract the two times to get seconds
09 $time = $time_end - $time_start;
10   
11 echo 'Script took '.$time.' seconds to execute';

[代码] PHP的维护模式

01 function maintenance($mode = FALSE){
02     if($mode){
03         if(basename($_SERVER['SCRIPT_FILENAME']) != 'maintenance.php'){
04             header("Location: ");
05             exit;
06         }
07     }else{
08         if(basename($_SERVER['SCRIPT_FILENAME']) == 'maintenance.php'){
09             header("Location: ");
10             exit;
11         }
12     }
13 }

[代码] 阻止CSS样式被缓存

1 "/stylesheet.css?" rel="stylesheet" type="text/css" /&glt;

[代码] 为数字增加 st\nd\rd 等

01 function make_ranked($rank) {
02     $last = substr( $rank, -1 );
03     $seclast = substr( $rank, -2, -1 );
04     if( $last > 3 || $last == 0 ) $ext = 'th';
05     else if( $last == 3 ) $ext = 'rd';
06     else if( $last == 2 ) $ext = 'nd';
07     else $ext = 'st'
08   
09     if( $last == 1 && $seclast == 1) $ext = 'th';
10     if( $last == 2 && $seclast == 1) $ext = 'th';
11     if( $last == 3 && $seclast == 1) $ext = 'th'
12   
13     return $rank.$ext;
14 }
阅读(468) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~