您好,欢迎访问一九零五行业门户网

PHP开发中常用的十个代码样例,php开发十个样例_PHP教程

php开发中常用的十个代码样例,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;
以上内容针对php开发中常用的十个代码样例做了总结,希望对大家有所帮助。
您可能感兴趣的文章:phpquery让php处理html代码像jquery一样方便php计算数组相同值出现次数的代码(array_count_values)php 实现代码复用的一个方法 traits新特性使用gdb调试php代码,解决php代码死循环问题mysql数据库查询结果用表格输出php代码示例php curl请求信息和返回信息设置代码实例
http://www.bkjia.com/phpjc/1098694.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/1098694.htmltecharticlephp开发中常用的十个代码样例,php开发十个样例 一、黑名单过滤 function is_spam($text, $file, $split = ‘:‘, $regex = false){ $handle = fopen($file, ‘rb‘...
其它类似信息

推荐信息