我们在开发中经常会要用到一些功能,下文小编整理了在开发应用中碰到的一些php经典代码了,下面一起来看看.
一、黑名单过滤,代码如下:
function is_spam($text, $file, $split = ':', $regex = false){ $handle = fopen($file, 'rb'); $contents = fread($handle, filesize($file)); fclose($handle); $lines = explode(n, $contents); $arr = array(); foreach($lines as $line){ list($word, $count) = explode($split, $line); if($regex) $arr[$word] = $count; else $arr[preg_quote($word)] = $count; } preg_match_all(~.implode('|', array_keys($arr)).~, $text, $matches); $temp = array(); foreach($matches[0] as $match){ if(!in_array($match, $temp)){ $temp[$match] = $temp[$match] + 1; if($temp[$match] >= $arr[$word]) return true; } } return false; }$file = 'spam.txt'; $str = 'this string has cat, dog word'; if(is_spam($str, $file)) echo 'this is spam'; else echo 'this is not spam'; ab:3 dog:3 cat:2 monkey:2
二、随机颜色生成器,代码如下:
function randomcolor() { $str = '#'; for($i = 0 ; $i 0){ $row = mysql_fetch_array($sql); // set some headers header(pragma: public); header(expires: 0); header(cache-control: must-revalidate, post-check=0, pre-check=0); header(content-type: application/force-download); header(content-type: application/octet-stream); header(content-type: application/download); header(content-disposition: attachment; filename=.basename($row['filename']).;); header(content-transfer-encoding: binary); header(content-length: .filesize($row['filename'])); @readfile($row['filename']); exit(0); }else{ header(location: /); exit; }
六、用email显示用户的gravator头像
$gravatar_link = 'http://www.gravatar.com/avatar/' . md5($comment_author_email) . '?s=32'; echo '';
七、用curl获取rss订阅数,代码如下:
$ch = curl_init(); curl_setopt($ch,curlopt_url,'https://feedburner.google.com/api/awareness/1.0/getfeeddata?id=7qkrmib4r9rscbplq5qgadiiq4'); curl_setopt($ch,curlopt_returntransfer,1); curl_setopt($ch,curlopt_connecttimeout,2); $content = curl_exec($ch); $subscribers = get_match('/circulation=(.*)/isu',$content); curl_close($ch);
八、时间差异计算,代码如下:
function ago($time) { $periods = array(second, minute, hour, day, week, month, year, decade); $lengths = array(60,60,24,7,4.35,12,10); $now = time(); $difference = $now - $time; $tense = ago; for($j = 0; $difference >= $lengths[$j] && $j =200 && $httpcode<300) return true; else return false; } if (visit(http://www.google.com)) echo website ok.n; else echo website down;
本文地址:
转载随意,但请附上文章地址:-)
