当前位置:Gxlcms > PHP教程 > 10个典型实用的PHP代码片段_PHP教程

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

时间:2021-07-01 10:21:17 帮助过:6人阅读

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


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

一、黑名单过滤

  1. function is_spam($text, $file, $split = ':https://www.gxlcms.com/', $regex = false){
  2. $handle = fopen($file, 'rbhttps://www.gxlcms.com/');
  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. }
  14. preg_match_all("~".implode('|https://www.gxlcms.com/', array_keys($arr))."~", $text, $matches);
  15. $temp = array();
  16. foreach($matches[0] as $match){
  17. if(!in_array($match, $temp)){
  18. $temp[$match] = $temp[$match] + 1;
  19. if($temp[$match] >= $arr[$word])
  20. return true;
  21. }
  22. }
  23. return false;
  24. }
  25. $file = 'spam.txthttps://www.gxlcms.com/';
  26. $str = 'This string has cat, dog wordhttps://www.gxlcms.com/';
  27. if(is_spam($str, $file))
  28. echo 'this is spamhttps://www.gxlcms.com/';
  29. else
  30. echo 'this is not spamhttps://www.gxlcms.com/';
  31. ab:3
  32. dog:3
  33. cat:2
  34. monkey:2

二、随机颜色生成器

  1. function randomColor() {
  2. $str = '#https://www.gxlcms.com/';
  3. for($i = 0 ; $i < 6 ; $i++) {
  4. $randNum = rand(0 , 15);
  5. switch ($randNum) {
  6. case 10: $randNum = 'Ahttps://www.gxlcms.com/'; break;
  7. case 11: $randNum = 'Bhttps://www.gxlcms.com/'; break;
  8. case 12: $randNum = 'Chttps://www.gxlcms.com/'; break;
  9. case 13: $randNum = 'Dhttps://www.gxlcms.com/'; break;
  10. case 14: $randNum = 'Ehttps://www.gxlcms.com/'; break;
  11. case 15: $randNum = 'Fhttps://www.gxlcms.com/'; break;
  12. }
  13. $str .= $randNum;
  14. }
  15. return $str;
  16. }
  17. $color = randomColor();

三、从网上下载文件

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

四、Alexa/Google Page Rank

  1. function page_rank($page, $type = 'alexahttps://www.gxlcms.com/'){
  2. switch($type){
  3. case 'alexahttps://www.gxlcms.com/':
  4. $url = 'http://alexa.com/siteinfo/https://www.gxlcms.com/';
  5. $handle = fopen($url.$page, 'rhttps://www.gxlcms.com/');
  6. break;
  7. case 'googlehttps://www.gxlcms.com/':
  8. $url = 'http://google.com/search?client=navclient-auto&ch=6-1484155081&features=Rank&q=info:https://www.gxlcms.com/';
  9. $handle = fopen($url.'http://https://www.gxlcms.com/'.$page, 'rhttps://www.gxlcms.com/');
  10. break;
  11. }
  12. $content = stream_get_contents($handle);
  13. fclose($handle);
  14. $content = preg_replace("~(n|t|ss+)~",'https://www.gxlcms.com/', $content);
  15. switch($type){
  16. case 'alexahttps://www.gxlcms.com/':
  17. if(preg_match('~(.+?) ~imhttps://www.gxlcms.com/',$content,$matches)){
  18. return $matches[2];
  19. }else{
  20. return FALSE;
  21. }
  22. break;
  23. case 'googlehttps://www.gxlcms.com/':
  24. $rank = explode(':https://www.gxlcms.com/',$content);
  25. if($rank[2] != 'https://www.gxlcms.com/')
  26. return $rank[2];
  27. else
  28. return FALSE;
  29. break;
  30. default:
  31. return FALSE;
  32. break;
  33. }
  34. }
  35. // Alexa Page Rank:
  36. echo 'Alexa Rank: https://www.gxlcms.com/'.page_rank('techug.comhttps://www.gxlcms.com/');
  37. echo ' https://www.gxlcms.com/';
  38. // Google Page Rank
  39. echo 'Google Rank: https://www.gxlcms.com/'.page_rank('techug.comhttps://www.gxlcms.com/', 'googlehttps://www.gxlcms.com/');

五、强制下载文件

  1. $filename = $_GET['filehttps://www.gxlcms.com/']; //Get the fileid from the URL
  2. // Query the file ID
  3. $query = sprintf("SELECT * FROM tableName WHERE id = '%s'",mysql_real_escape_string($filename));
  4. $sql = mysql_query($query);
  5. if(mysql_num_rows($sql) > 0){
  6. $row = mysql_fetch_array($sql);
  7. // Set some headers
  8. header("Pragma: public");
  9. header("Expires: 0");
  10. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  11. header("Content-Type: application/force-download");
  12. header("Content-Type: application/octet-stream");
  13. header("Content-Type: application/download");
  14. header("Content-Disposition: attachment; filename=".basename($row['FileNamehttps://www.gxlcms.com/']).";");
  15. header("Content-Transfer-Encoding: binary");
  16. header("Content-Length: ".filesize($row['FileNamehttps://www.gxlcms.com/']));
  17. @readfile($row['FileNamehttps://www.gxlcms.com/']);
  18. exit(0);
  19. }else{
  20. header("Location: /");
  21. exit;
  22. }

六、用Email显示用户的Gravator头像

  1. $gravatar_link = 'http://www.gravatar.com/avatar/https://www.gxlcms.com/' . md5($comment_author_email) . '?s=32https://www.gxlcms.com/';
  2. echo '$gravatar_link . '" />https://www.gxlcms.com/';

七、用cURL获取RSS订阅数

  1. $ch = curl_init();
  2. curl_setopt($ch,CURLOPT_URL,'https://feedburner.google.com/api/awareness/1.0/GetFeedData?id=7qkrmib4r9rscbplq5qgadiiq4https://www.gxlcms.com/');
  3. curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  4. curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,2);
  5. $content = curl_exec($ch);
  6. $subscribers = get_match('/circulation="(.*)"/isUhttps://www.gxlcms.com/',$content);
  7. curl_close($ch);

八、时间差异计算

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

九、截取图片

  1. $filename= "test.jpg";
  2. list($w, $h, $type, $attr) = getimagesize($filename);
  3. $src_im = imagecreatefromjpeg($filename);
  4. $src_x = '0https://www.gxlcms.com/'; // begin x
  5. $src_y = '0https://www.gxlcms.com/'; // begin y
  6. $src_w = '100https://www.gxlcms.com/'; // width
  7. $src_h = '100https://www.gxlcms.com/'; // height
  8. $dst_x = '0https://www.gxlcms.com/'; // destination x
  9. $dst_y = '0https://www.gxlcms.com/'; // destination y
  10. $dst_im = imagecreatetruecolor($src_w, $src_h);
  11. $white = imagecolorallocate($dst_im, 255, 255, 255);
  12. imagefill($dst_im, 0, 0, $white);
  13. imagecopy($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h);
  14. header("Content-type: image/png");
  15. imagepng($dst_im);
  16. 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. }
  18. if (Visit("http://www.google.com"))
  19. echo "Website OK"."n";
  20. else
  21. echo "Website DOWN";



www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1059208.htmlTechArticle10个典型实用的PHP代码片段 本文将介绍10个经常会用到的PHP代码片段,包括黑名单过滤、随机颜色生成器、从网上下载文件、Alexa/Google Pag...

人气教程排行