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

[代码]关键词高亮

 
 
  1. functionhighlight($sString,$aWords){

  2. if(!is_array($aWords)||emptyempty($aWords)||!is_string($sString)){

  3. returnfalse;

  4. }

  5. $sWords=implode('|',$aWords);

  6. returnpreg_replace('@\b('.$sWords.')\b@si','<strongstyle="background-color:yellow">$1</strong>',$sString);

  7. }

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

 
 
  1. functionget_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=newSimpleXMLElement($data);

  11. $fb=$xml->feed->entry['circulation'];

  12. $nb=0;

  13. foreach($xml->feed->children()as$circ){

  14. $nb+=$circ['circulation'];

  15. }

  16. returnround($nb/$interval);

  17. }

[代码]自动生成密码

 
 
  1. functiongeneratePassword($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. $password='';

  17. $alt=time()%2;

  18. for($i=0;$i<$length;$i++){

  19. if($alt==1){

  20. $password.=$consonants[(rand()%strlen($consonants))];

  21. $alt=0;

  22. }else{

  23. $password.=$vowels[(rand()%strlen($vowels))];

  24. $alt=1;

  25. }

  26. }

  27. return$password;

  28. }

[代码]压缩多个CSS文件

 
 
  1. header('Content-type:text/css');

  2. ob_start("compress");

  3. functioncompress($buffer){

  4. /*removecomments*/

  5. $buffer=preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!','',$buffer);

  6. /*removetabs,spaces,newlines,etc.*/

  7. $buffer=str_replace(array("\r\n","\r","\n","\t",'','',''),'',$buffer);

  8. return$buffer;

  9. }

  10. /*yourcssfiles*/

  11. include('master.css');

  12. include('typography.css');

  13. include('grid.css');

  14. include('print.css');

  15. include('handheld.css');

  16. ob_end_flush();

[代码]获取短网址

 
 
  1. functiongetTinyUrl($url){

  2. returnfile_get_contents("http://tinyurl.com/api-create.php?url=".$url);

  3. }

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

 
 
  1. functionage($date){

  2. $year_diff='';

  3. $time=strtotime($date);

  4. if(FALSE===$time){

  5. return'';

  6. }

  7. $date=date('Y-m-d',$time);

  8. list($year,$month,$day)=explode("-",$date);

  9. $year_diff=date("Y")�C$year;

  10. $month_diff=date("m")�C$month;

  11. $day_diff=date("d")�C$day;

  12. if($day_diff<0||$month_diff<0)$year_diff�C;

  13. return$year_diff;

  14. }

[代码]计算执行时间

 
 
  1. //Createavariableforstarttime

  2. $time_start=microtime(true);

  3. //PlaceyourPHP/HTML/JavaScript/CSS/Etc.Here

  4. //Createavariableforendtime

  5. $time_end=microtime(true);

  6. //Subtractthetwotimestogetseconds

  7. $time=$time_end-$time_start;

  8. echo'Scripttook'.$time.'secondstoexecute';

[代码]PHP的维护模式

 
 
  1. functionmaintenance($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. }

  13. }

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

 
 
  1. <linkhref="/stylesheet.css?<?phpechotime();?>"rel="stylesheet"type="text/css"/&glt;

[代码]为数字增加stndrd等

 
 
  1. functionmake_ranked($rank){

  2. $last=substr($rank,-1);

  3. $seclast=substr($rank,-2,-1);

  4. if($last>3||$last==0)$ext='th';

  5. elseif($last==3)$ext='rd';

  6. elseif($last==2)$ext='nd';

  7. else$ext='st';

  8. if($last==1&&$seclast==1)$ext='th';

  9. if($last==2&&$seclast==1)$ext='th';

  10. if($last==3&&$seclast==1)$ext='th';

  11. return$rank.$ext;

  12. }

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

你可能感兴趣的:(PHP,highlight)