web开发中常用的PHP代码片段

2014-03-13 18:51:41

1.关键词高亮

function highlight($sString, $aWords) { if (!isarray ($aWords) || empty ($aWords) || !isstring ($sString)) { return false; } $sWords = implode ('|', $aWords); return preg_replace ('@\b('.$sWords.')\b@si', '$1', $sString); }

2.获取你的Feedburner的用户

function getaveragereaders($feedid,$interval = 7){ $today = date('Y-m-d', strtotime("now")); $ago = date('Y-m-d', strtotime("-".$interval." days")); $feedurl="https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=".$feedid."&dates=".$ago.",".$today; $ch = curlinit(); curlsetopt($ch, CURLOPTRETURNTRANSFER, 1); curlsetopt($ch, CURLOPTURL, $feedurl); $data = curlexec($ch); curl_close($ch); $xml = new SimpleXMLElement($data); $fb = $xml->feed->entry['circulation']; $nb = 0; foreach($xml->feed->children() as $circ){ $nb += $circ['circulation']; } return round($nb/$interval); }

3.自动生成密码

function generatePassword($length=9, $strength=0) { $vowels = 'aeuy'; $consonants = 'bdghjmnpqrstvz'; if ($strength >= 1) { $consonants .= 'BDGHJLMNPQRSTVWXZ'; } if ($strength >= 2) { $vowels .= "AEUY"; } if ($strength >= 4) { $consonants .= '23456789'; } if ($strength >= 8 ) { $vowels .= '@#$%'; } $password = ''; $alt = time() % 2; for ($i = 0; $i < $length; $i++) { if ($alt 1) { $password .= $consonants[(rand() % strlen($consonants))]; $alt = 0; } else { $password .= $vowels[(rand() % strlen($vowels))]; $alt = 1; } } return $password; }

4.压缩多个CSS文件

header('Content-type: text/css'); obstart("compress"); function compress($buffer) { /* remove comments */ $buffer = pregreplace('!/*[^]*+([^/][^]*+)/!', '', $buffer); / remove tabs, spaces, newlines, etc. / $buffer = str_replace(array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), '', $buffer); return $buffer; } / your css files */ include('master.css'); include('typography.css'); include('grid.css'); include('print.css'); include('handheld.css'); obendflush();

5.获取短网址

function getTinyUrl($url) { return filegetcontents("http://tinyurl.com/api-create.php?url=".$url); }

6.根据生日计算年龄

function age($date){ $yeardiff = ''; $time = strtotime($date); if(FALSE = $time){ return ''; } $date = date('Y-m-d', $time); list($year,$month,$day) = explode("-",$date); $yeardiff = date("Y") – $year; $monthdiff = date("m") – $month; $daydiff = date("d") – $day; if ($daydiff < 0 || $monthdiff < 0) $yeardiff–; return $yeardiff; }

7.计算执行时间

//Create a variable for start time $timestart = microtime(true); // Place your PHP/HTML/JavaScript/CSS/Etc. Here //Create a variable for end time $timeend = microtime(true); //Subtract the two times to get seconds $time = $timeend - $timestart; echo 'Script took '.$time.' seconds to execute';

8.PHP的维护模式

function maintenance($mode = FALSE){ if($mode){ if(basename($SERVER['SCRIPTFILENAME']) != 'maintenance.php'){ header("Location: http://example.com/maintenance.php"); exit; } }else{ if(basename($SERVER['SCRIPTFILENAME']) 'maintenance.php'){ header("Location: http://example.com/"); exit; } } }

9.阻止CSS样式被缓存

10.数字增加 st\nd\rd 等

function make_ranked($rank) { $last = substr( $rank, -1 ); $seclast = substr( $rank, -2, -1 ); if( $last > 3 || $last 0 ) $ext = 'th'; else if( $last 3 ) $ext = 'rd'; else if( $last 2 ) $ext = 'nd'; else $ext = 'st'; if( $last 1 && $seclast 1) $ext = 'th'; if( $last 2 && $seclast 1) $ext = 'th'; if( $last 3 && $seclast 1) $ext = 'th'; return $rank.$ext; }

玉兰花开

下午没课,这学期还挺闲的。楼下的玉兰花又该开了,去年我给她拍的照片还躺在电脑里。二楼的风不大不小,凉凉的很舒服。生活真美好!早上开班会竞选班委,大家都很积极,我总是在这种事上很消极,长辈们见了也少不了要问问有没有任职,在他们眼里我应该是个胆怯的孩子吧!我只是不好这个…读一位朋友的日记,他说他这几天辞掉了兼职,他说兼职占据了他的空闲时间,以后半辈子都在工作,为什么要用他短暂的读书年龄去兼职呢,他要干点自己喜欢的事,别以后想起来,从大学开始就一直在工作。他喜欢画画,我看了他的作品,我很喜欢,我又想起了自己的爱好。一直爱画画,原来还学过素描,好像是到高中后就慢慢没时间了,还写过毛笔字,最后没坚持下来~高中喜欢写诗词,虽然都是玩的,谈不得好坏,那本应该还在,等什么时候回老家了翻出来再看看。现在是没那闲情逸趣了,现在喜欢瞎鼓捣,这点代码,折腾会电脑。我不知道这算不算爱好,管他呢,我喜欢就好,偶尔接点项目赚点小钱画画也挺不错的~社团里有个小姑娘,话也不多,昨天看她的博客,便能看到她的思想,她期待大学带给她全新的环境,她能努力展现自己,过的快乐充实。

前端设计字体大全

宋体 SimSun 黑体 SimHei 微软雅黑 Microsoft YaHei 微软正黑体 Microsoft JhengHei 新宋体 NSimSun 新细明体 PMingLiU 细明体 MingLiU 标楷体 DFKai-SB 仿宋 FangSong 楷体 KaiTi 仿宋GB2312 FangSongGB2312 楷体GB2312 KaiTiGB2312 宋体:SimSuncss中中文字体(font-family)的英文名称 Mac OS的一些: 华文细黑:STHeiti Light [STXihei] 华文黑体:STHeiti 华文楷体:STKaiti 华文宋体:STSong 华文仿宋:STFangsong 俪黑 Pro:LiHei Pro Medium 俪宋