0

0

10个必须收藏的PHP代码样例

php中文网

php中文网

发布时间:2016-06-21 08:46:27

|

1130人浏览过

|

来源于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. }
  14. preg_match_all("~".implode('', 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.txt';
  26. $str = 'This string has cat, dog word';
  27. if(is_spam($str, $file))
  28. echo 'this is spam';
  29. else
  30. echo 'this is not spam';
  31. ab:3
  32. dog:3
  33. cat:2
  34. 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. }
  17. $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. // 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.'.'.$ext;
  22. if(preg_match("/[^0-9a-z._-]/i", $saveFile))
  23. $saveFile = md5(microtime(true)).'.'.$ext;
  24. $handle = fopen($saveFile, 'wb');
  25. fwrite($handle, $opt);
  26. fclose($handle);

四、Alexa/Google Page Rank

立即学习PHP免费学习笔记(深入)”;

  1. function page_rank($page, $type = 'alexa'){
  2. switch($type){
  3. case 'alexa':
  4. $url = 'http://alexa.com/siteinfo/';
  5. $handle = fopen($url.$page, 'r');
  6. break;
  7. case 'google':
  8. $url = 'http://google.com/search?client=navclient-auto&ch=6-1484155081&features=Rank&q=info:';
  9. $handle = fopen($url.'http://'.$page, 'r');
  10. break;
  11. }
  12. $content = stream_get_contents($handle);
  13. fclose($handle);
  14. $content = preg_replace("~(ntss+)~",'', $content);
  15. switch($type){
  16. case 'alexa':
  17. if(preg_match('~
    @@##@@(.+?)
    ~im'
    ,$content,$matches)){
  18. return $matches[2];
  19. }else{
  20. return FALSE;
  21. }
  22. break;
  23. case 'google':
  24. $rank = explode(':',$content);
  25. if($rank[2] != '')
  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: '.page_rank('techug.com');
  37. echo ' ';
  38. // Google Page Rank
  39. echo 'Google Rank: '.page_rank('techug.com', 'google');

五、强制下载文件

WHEE
WHEE

WHEE是一款AI绘画与图片生成器,提供一站式AI视觉创作服务。WHEE不仅会画也会修图,各种AI修图功能一应俱全。

下载
  1. $filename = $_GET['file']; //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['FileName']).";");
  15. header("Content-Transfer-Encoding: binary");
  16. header("Content-Length: ".filesize($row['FileName']));
  17. @readfile($row['FileName']);
  18. exit(0);
  19. }else{
  20. header("Location: /");
  21. exit;
  22. }

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

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

七、用cURL获取RSS订阅数

  1. $ch = curl_init();
  2. curl_setopt($ch,CURLOPT_URL,'https://feedburner.google.com/api/awareness/1.0/GetFeedData?id=7qkrmib4r9rscbplq5qgadiiq4');
  3. curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  4. curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,2);
  5. $content = curl_exec($ch);
  6. $subscribers = get_match('/circulation="(.*)"/isU',$content);
  7. curl_close($ch);
  8. 八、时间差异计算
  9. function ago($time)
  10. {
  11. $periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
  12. $lengths = array("60","60","24","7","4.35","12","10");
  13. $now = time();
  14. $difference = $now - $time;
  15. $tense = "ago";
  16. for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
  17. $difference /= $lengths[$j];
  18. }
  19. $difference = round($difference);
  20. if($difference != 1) {
  21. $periods[$j].= "s";
  22. }
  23. return "$difference $periods[$j] 'ago' ";
  24. }

九、截取图片

  1. $filename= "test.jpg";
  2. list($w, $h, $type, $attr) = getimagesize($filename);
  3. $src_im = imagecreatefromjpeg($filename);
  4. $src_x = '0'; // begin x
  5. $src_y = '0'; // begin y
  6. $src_w = '100'; // width
  7. $src_h = '100'; // height
  8. $dst_x = '0'; // destination x
  9. $dst_y = '0'; // 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";

 

【责任编辑:wangxueyan TEL:(010)68476606】



10个必须收藏的PHP代码样例10个必须收藏的PHP代码样例
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

相关标签:

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
2026春节习俗大全
2026春节习俗大全

本专题整合了2026春节习俗大全,阅读专题下面的文章了解更多详细内容。

68

2026.02.11

Yandex网页版官方入口使用指南_国际版与俄罗斯版访问方法解析
Yandex网页版官方入口使用指南_国际版与俄罗斯版访问方法解析

本专题全面整理了Yandex搜索引擎的官方入口信息,涵盖国际版与俄罗斯版官网访问方式、网页版直达入口及免登录使用说明,帮助用户快速、安全地进入Yandex官网,高效使用其搜索与相关服务。

200

2026.02.11

虫虫漫画网页版入口与免费阅读指南_正版漫画全集在线查看方法
虫虫漫画网页版入口与免费阅读指南_正版漫画全集在线查看方法

本专题系统整理了虫虫漫画官网及网页版最新入口,涵盖免登录观看、正版漫画全集在线阅读方式,并汇总稳定可用的访问渠道,帮助用户快速找到虫虫漫画官方页面,轻松在线阅读各类热门漫画内容。

40

2026.02.11

Docker容器化部署与DevOps实践
Docker容器化部署与DevOps实践

本专题面向后端与运维开发者,系统讲解 Docker 容器化技术在实际项目中的应用。内容涵盖 Docker 镜像构建、容器运行机制、Docker Compose 多服务编排,以及在 DevOps 流程中的持续集成与持续部署实践。通过真实场景演示,帮助开发者实现应用的快速部署、环境一致性与运维自动化。

4

2026.02.11

Rust异步编程与Tokio运行时实战
Rust异步编程与Tokio运行时实战

本专题聚焦 Rust 语言的异步编程模型,深入讲解 async/await 机制与 Tokio 运行时的核心原理。内容包括异步任务调度、Future 执行模型、并发安全、网络 IO 编程以及高并发场景下的性能优化。通过实战示例,帮助开发者使用 Rust 构建高性能、低延迟的后端服务与网络应用。

1

2026.02.11

Spring Boot企业级开发与MyBatis Plus实战
Spring Boot企业级开发与MyBatis Plus实战

本专题面向 Java 后端开发者,系统讲解如何基于 Spring Boot 与 MyBatis Plus 构建高效、规范的企业级应用。内容涵盖项目架构设计、数据访问层封装、通用 CRUD 实现、分页与条件查询、代码生成器以及常见性能优化方案。通过完整实战案例,帮助开发者提升后端开发效率,减少重复代码,快速交付稳定可维护的业务系统。

6

2026.02.11

包子漫画网页版入口与全集阅读指南_正版免费漫画快速访问方法
包子漫画网页版入口与全集阅读指南_正版免费漫画快速访问方法

本专题汇总了包子漫画官网和网页版入口,提供最新章节抢先看方法、正版免费阅读指南,以及稳定访问方式,帮助用户快速直达包子漫画页面,无广告畅享全集漫画内容。

159

2026.02.10

MC.JS网页版快速畅玩指南_MC.JS官网在线入口及免安装体验方法
MC.JS网页版快速畅玩指南_MC.JS官网在线入口及免安装体验方法

本专题汇总了MC.JS官网入口和网页版快速畅玩方法,提供免安装访问、不同版本(1.8.8、1.12.8)在线体验指南,以及正版网页端操作说明,帮助玩家轻松进入MC.JS世界,实现即时畅玩与高效体验。

89

2026.02.10

谷歌邮箱网页版登录与注册全指南_Gmail账号快速访问与安全操作教程
谷歌邮箱网页版登录与注册全指南_Gmail账号快速访问与安全操作教程

本专题汇总了谷歌邮箱网页版的最新登录入口和注册方法,详细提供官方账号快速访问方式、网页版操作教程及安全登录技巧,帮助用户轻松管理Gmail邮箱账户,实现高效、安全的邮箱使用体验。

78

2026.02.10

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Excel 教程
Excel 教程

共162课时 | 16.8万人学习

Pandas 教程
Pandas 教程

共15课时 | 1.1万人学习

C# 教程
C# 教程

共94课时 | 9.2万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号