最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
值得收藏的10个PHP代码样例(黑名单\下载文件)
时间:2015-01-06 编辑:简简单单 来源:一聚教程网
一、黑名单过滤
代码如下 | 复制代码 |
function is_spam($text, $file, $split = ':', $regex = false){ $file = 'spam.txt'; |
二、随机颜色生成器
代码如下 | 复制代码 |
function randomColor() { $str = '#'; for($i = 0 ; $i < 6 ; $i++) { $randNum = rand(0 , 15); switch ($randNum) { case 10: $randNum = 'A'; break; case 11: $randNum = 'B'; break; case 12: $randNum = 'C'; break; case 13: $randNum = 'D'; break; case 14: $randNum = 'E'; break; case 15: $randNum = 'F'; break; } $str .= $randNum; } return $str; } $color = randomColor(); |
三、从网上下载文件
代码如下 | 复制代码 |
set_time_limit(0); // create a new cURL resource // set URL and other appropriate options // grab URL and pass it to the browser // close cURL resource, and free up system resources $saveFile = $name.'.'.$ext; $handle = fopen($saveFile, 'wb'); |
四、Alexa/Google Page Rank
代码如下 | 复制代码 |
function page_rank($page, $type = 'alexa'){ switch($type){ case 'alexa': $url = 'http://alexa.com/siteinfo/'; $handle = fopen($url.$page, 'r'); break; case 'google': $url = 'http://google.com/search?client=navclient-auto&ch=6-1484155081&features=Rank&q=info:'; $handle = fopen($url.'http://'.$page, 'r'); break; } $content = stream_get_contents($handle); fclose($handle); $content = preg_replace("~(n|t|ss+)~",'', $content); switch($type){ case 'alexa': if(preg_match('~ return $matches[2]; }else{ return FALSE; } break; case 'google': $rank = explode(':',$content); if($rank[2] != '') return $rank[2]; else return FALSE; break; default: return FALSE; break; } } // Alexa Page Rank: echo 'Alexa Rank: '.page_rank('techug.com'); echo ' '; // Google Page Rank echo 'Google Rank: '.page_rank('techug.com', 'google'); |
五、强制下载文件
七、用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) $now = time(); $difference = $now - $time; for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) { $difference = round($difference); if($difference != 1) { return "$difference $periods[$j] 'ago' "; |
九、截取图片
代码如下 | 复制代码 |
$filename= "test.jpg"; $src_x = '0'; // begin x $dst_im = imagecreatetruecolor($src_w, $src_h); imagecopy($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h); header("Content-type: image/png"); |
十、检查网站是否宕机
代码如下 | 复制代码 |
function Visit($url){ |
相关文章
- PHP导出数据超时的优化建议解读 10-31
- PHP之mysql位运算解析 10-31
- Laravel实现登录跳转功能解析 10-31
- php双向队列解读 10-31
- Laravel异常上下文解决教程 10-24
- php数组查询元素位置方法介绍 10-24