加入收藏 | 设为首页 | 会员中心 | 我要投稿 三明站长网 (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

八、时间差异计算

  1. function ago($time) 
  2.    $periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade"); 
  3. $lengths = array("60","60","24","7","4.35","12","10"); 
  4.  
  5. $now = time(); 
  6.  
  7. $difference = $now - $time; 
  8. $tense = "ago"; 
  9.  
  10. for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) { 
  11. $difference /= $lengths[$j]; 
  12.  
  13. $difference = round($difference); 
  14.  
  15. if($difference != 1) { 
  16. $periods[$j].= "s"; 
  17.  
  18. return "$difference $periods[$j] 'ago' "; 

九、截取图片

  1. $filename= "test.jpg"; 
  2. list($w, $h, $type, $attr) = getimagesize($filename); 
  3. $src_im = imagecreatefromjpeg($filename); 
  4.  
  5. $src_x = '0'; // begin x 
  6. $src_y = '0'; // begin y 
  7. $src_w = '100'; // width 
  8. $src_h = '100'; // height 
  9. $dst_x = '0'; // destination x 
  10. $dst_y = '0'; // destination y 
  11.  
  12. $dst_im = imagecreatetruecolor($src_w, $src_h); 
  13. $white = imagecolorallocate($dst_im, 255, 255, 255); 
  14. imagefill($dst_im, 0, 0, $white); 
  15.  
  16. imagecopy($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h); 
  17.  
  18. header("Content-type: image/png"); 
  19. imagepng($dst_im); 
  20. imagedestroy($dst_im); 

十、检查网站是否宕机

  1. function Visit($url){ 
  2.        $agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";$ch=curl_init(); 
  3. curl_setopt ($ch, CURLOPT_URL,$url ); 
  4. curl_setopt($ch, CURLOPT_USERAGENT, $agent); 
  5. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
  6. curl_setopt ($ch,CURLOPT_VERBOSE,false); 
  7. curl_setopt($ch, CURLOPT_TIMEOUT, 5); 
  8. curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, FALSE); 
  9. curl_setopt($ch,CURLOPT_SSLVERSION,3); 
  10. curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, FALSE); 
  11. $page=curl_exec($ch); 
  12. //echo curl_error($ch); 
  13. $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
  14. curl_close($ch); 
  15. if($httpcode>=200 && $httpcode<300) return true; 
  16. else return false; 
  17. if (Visit("http://www.google.com")) 
  18. echo "Website OK"."n"; 
  19. else 
  20. echo "Website DOWN"; 

(编辑:三明站长网)

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

热点阅读