10 个救命的 PHP 代码片段

[代码] 关键词高亮

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''<strong style="background-color:yellow">$1</strong>'$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="https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=".$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

你可能感兴趣的:(10 个救命的 PHP 代码片段)