加入收藏 | 设为首页 | 会员中心 | 我要投稿 三明站长网 (https://www.0598zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

10个典型实用的PHP代码片段

发布时间:2016-09-28 15:28:07 所属栏目:PHP教程 来源:淡忘~浅思
导读:副标题#e# 本文将介绍10个经常会用到的PHP代码片段,包括黑名单过滤、随机颜色生成器、从网上下载文件、Alexa/Google Page Rank、强制下载文件、用Email显示用户的Gravator头像、用cURL获取RSS订阅数、截取图片、检查网站是否宕机。 一、黑名单过滤 functio
副标题[/!--empirenews.page--]

本文将介绍10个经常会用到的PHP代码片段,包括黑名单过滤、随机颜色生成器、从网上下载文件、Alexa/Google Page Rank、强制下载文件、用Email显示用户的Gravator头像、用cURL获取RSS订阅数、截取图片、检查网站是否宕机。

10个典型实用的PHP代码片段

一、黑名单过滤

  1. function is_spam($text, $file, $split = ':', $regex = false){ 
  2.     $handle = fopen($file, 'rb'); 
  3.     $contents = fread($handle, filesize($file)); 
  4.     fclose($handle); 
  5.     $lines = explode("n", $contents); 
  6. $arr = array(); 
  7. foreach($lines as $line){ 
  8. list($word, $count) = explode($split, $line); 
  9. if($regex) 
  10. $arr[$word] = $count; 
  11. else 
  12. $arr[preg_quote($word)] = $count; 
  13. preg_match_all("~".implode('|', array_keys($arr))."~", $text, $matches); 
  14. $temp = array(); 
  15. foreach($matches[0] as $match){ 
  16. if(!in_array($match, $temp)){ 
  17. $temp[$match] = $temp[$match] + 1; 
  18. if($temp[$match] >= $arr[$word]) 
  19. return true; 
  20. return false; 
  21.  
  22. $file = 'spam.txt'; 
  23. $str = 'This string has cat, dog word'; 
  24. if(is_spam($str, $file)) 
  25. echo 'this is spam'; 
  26. else 
  27. echo 'this is not spam'; 
  28.  
  29. ab:3 
  30. dog:3 
  31. cat:2 
  32. monkey:2 

二、随机颜色生成器

  1. function randomColor() { 
  2.     $str = '#'; 
  3.     for($i = 0 ; $i < 6 ; $i++) { 
  4.         $randNum = rand(0 , 15); 
  5.         switch ($randNum) { 
  6.             case 10: $randNum = 'A'; break; 
  7.             case 11: $randNum = 'B'; break; 
  8.             case 12: $randNum = 'C'; break; 
  9.             case 13: $randNum = 'D'; break; 
  10.             case 14: $randNum = 'E'; break; 
  11.             case 15: $randNum = 'F'; break; 
  12.         } 
  13.         $str .= $randNum; 
  14.     } 
  15.     return $str; 
  16. $color = randomColor(); 

三、从网上下载文件

  1. set_time_limit(0); 
  2. // Supports all file types 
  3. // URL Here: 
  4. $url = 'http://somsite.com/some_video.flv'; 
  5. $pi = pathinfo($url); 
  6. $ext = $pi['extension']; 
  7. $name = $pi['filename']; 
  8.  
  9. // create a new cURL resource 
  10. $ch = curl_init(); 
  11.  
  12. // set URL and other appropriate options 
  13. curl_setopt($ch, CURLOPT_URL, $url); 
  14. curl_setopt($ch, CURLOPT_HEADER, false); 
  15. curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); 
  16. curl_setopt($ch, CURLOPT_AUTOREFERER, true); 
  17. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
  18. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
  19.  
  20. // grab URL and pass it to the browser 
  21. $opt = curl_exec($ch); 
  22.  
  23. // close cURL resource, and free up system resources 
  24. curl_close($ch); 
  25.  
  26. $saveFile = $name.'.'.$ext; 
  27. if(preg_match("/[^0-9a-z._-]/i", $saveFile)) 
  28. $saveFile = md5(microtime(true)).'.'.$ext; 
  29.  
  30. $handle = fopen($saveFile, 'wb'); 
  31. fwrite($handle, $opt); 
  32. fclose($handle); 

(编辑:三明站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读