Web开发者必备的10个救命的PHP代码片段

开发 后端 前端
这些代码在Web开发经常会用到,关键时候救命!

[代码] 关键词高亮

  1. function highlight($sString$aWords) {  
  2.     if (!is_array ($aWords) || emptyempty ($aWords) || !is_string ($sString)) {  
  3.         return false;  
  4.     }  
  5.  
  6.     $sWords = implode ('|'$aWords);  
  7.     return preg_replace ('@\b('.$sWords.')\b@si''<strong style="background-color:yellow">$1</strong>'$sString);  

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

  1. function get_average_readers($feed_id,$interval = 7){  
  2.     $today = date('Y-m-d'strtotime("now"));  
  3.     $ago = date('Y-m-d'strtotime("-".$interval." days"));  
  4.     $feed_url="https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=".$feed_id."&dates=".$ago.",".$today;  
  5.     $ch = curl_init();  
  6.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
  7.     curl_setopt($ch, CURLOPT_URL, $feed_url);  
  8.     $data = curl_exec($ch);  
  9.     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);  

[代码] 自动生成密码

  1. function generatePassword($length=9, $strength=0) {  
  2.     $vowels = 'aeuy';  
  3.     $consonants = 'bdghjmnpqrstvz';  
  4.     if ($strength >= 1) {  
  5.         $consonants .= 'BDGHJLMNPQRSTVWXZ';  
  6.     }  
  7.     if ($strength >= 2) {  
  8.         $vowels .= "AEUY";  
  9.     }  
  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;  

[代码] 压缩多个CSS文件

  1. header('Content-type: text/css');  
  2. ob_start("compress");  
  3. function compress($buffer) {  
  4.   /* remove comments */ 
  5.   $buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!'''$buffer);  
  6.   /* remove tabs, spaces, newlines, etc. */ 
  7.   $buffer = str_replace(array("\r\n""\r""\n""\t"'  ''    ''    '), ''$buffer);  
  8.   return $buffer;  
  9. }  
  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("http://tinyurl.com/api-create.php?url=".$url);  

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

  1. function age($date){  
  2.     $year_diff = '';  
  3.     $time = strtotime($date);  
  4.     if(FALSE === $time){  
  5.         return '';  
  6.     }  
  7.  
  8.     $date = date('Y-m-d'$time);  
  9.     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;  

[代码] 计算执行时间

  1. //Create a variable for start time  
  2. $time_start = microtime(true);  
  3.  
  4. // Place your PHP/HTML/JavaScript/CSS/Etc. Here  
  5.  
  6. //Create a variable for end time  
  7. $time_end = microtime(true);  
  8. //Subtract the two times to get seconds  
  9. $time = $time_end - $time_start;  
  10.  
  11. echo 'Script took '.$time.' seconds to execute'

[代码] PHP的维护模式

  1. function maintenance($mode = FALSE){  
  2.     if($mode){  
  3.         if(basename($_SERVER['SCRIPT_FILENAME']) != 'maintenance.php'){  
  4.             header("Location: http://example.com/maintenance.php");  
  5.             exit;  
  6.         }  
  7.     }else{  
  8.         if(basename($_SERVER['SCRIPT_FILENAME']) == 'maintenance.php'){  
  9.             header("Location: http://example.com/");  
  10.             exit;  
  11.         }  
  12.     }  

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

  1. <link href="/stylesheet.css?<?php echo time(); ?>" rel="stylesheet" type="text/css" /&glt; 

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

  1. function make_ranked($rank) {  
  2.     $last = substr$rank, -1 );  
  3.     $seclast = substr$rank, -2, -1 );  
  4.     if$last > 3 || $last == 0 ) $ext = 'th';  
  5.     else if$last == 3 ) $ext = 'rd';  
  6.     else if$last == 2 ) $ext = 'nd';  
  7.     else $ext = 'st';   
  8.  
  9.     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;  

原文:http://www.oschina.net/code/snippet_12_6024

【编辑推荐】

  1. PHP环境安装套件:快速安装LAMP环境
  2. 10个免费的PHP编辑器/开发工具推荐
  3. 10个PHP分页技术的代码和示例
  4. Perl、PHP、Python、Java和Ruby的比较
  5. 谈PHP发展前景和就业解析
责任编辑:陈贻新 来源: 开源中国社区
相关推荐

2011-01-10 10:57:33

WebPHPJavaScript

2014-02-12 10:46:00

WebJavaScript音频库

2015-10-08 08:53:46

PHP代码片段

2015-04-21 12:54:21

2011-03-17 15:25:31

2022-06-08 08:55:15

JavaScript代码前端

2011-10-31 15:08:54

Chrome插件Web设计开发

2015-03-17 14:31:53

Web开发web开发者云开发环境

2011-03-30 08:49:34

WebjQuery

2013-12-30 13:46:27

Android开发者

2017-10-23 09:27:47

2015-09-06 16:22:48

JavaScriptSublimeText

2014-02-01 21:31:10

JavaScriptJS框架

2021-04-08 10:40:24

前端工具代码

2014-07-10 10:15:41

代码代码库

2014-12-10 10:01:31

PHP

2011-03-01 13:10:06

WebjQueryHTML 5

2023-11-30 15:30:19

Python编程语言

2011-10-20 10:09:14

JavaScript

2015-08-11 11:01:22

设计原则开发者
点赞
收藏

51CTO技术栈公众号