6个超实用的PHP代码片段


Posted in PHP onAugust 10, 2015

一、黑名单过滤

function is_spam($text, $file, $split = ':', $regex = false){ 
  $handle = fopen($file, 'rb'); 
  $contents = fread($handle, filesize($file)); 
  fclose($handle); 
  $lines = explode("n", $contents); 
$arr = array(); 
foreach($lines as $line){ 
list($word, $count) = explode($split, $line); 
if($regex) 
$arr[$word] = $count; 
else 
$arr[preg_quote($word)] = $count; 
} 
preg_match_all("~".implode('|', array_keys($arr))."~", $text, $matches); 
$temp = array(); 
foreach($matches[0] as $match){ 
if(!in_array($match, $temp)){ 
$temp[$match] = $temp[$match] + 1; 
if($temp[$match] >= $arr[$word]) 
return true; 
} 
} 
return false; 
} 
 
$file = 'spam.txt'; 
$str = 'This string has cat, dog word'; 
if(is_spam($str, $file)) 
echo 'this is spam'; 
else 
echo 'this is not spam'; 
 
ab:3 
dog:3 
cat:2 
monkey:2

二、随机颜色生成器

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); 
// Supports all file types 
// URL Here: 
$url = 'http://somsite.com/some_video.flv'; 
$pi = pathinfo($url); 
$ext = $pi['extension']; 
$name = $pi['filename']; 
 
// create a new cURL resource 
$ch = curl_init(); 
 
// set URL and other appropriate options 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_HEADER, false); 
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); 
curl_setopt($ch, CURLOPT_AUTOREFERER, true); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
 
// grab URL and pass it to the browser 
$opt = curl_exec($ch); 
 
// close cURL resource, and free up system resources 
curl_close($ch); 
 
$saveFile = $name.'.'.$ext; 
if(preg_match("/[^0-9a-z._-]/i", $saveFile)) 
$saveFile = md5(microtime(true)).'.'.$ext; 
 
$handle = fopen($saveFile, 'wb'); 
fwrite($handle, $opt); 
fclose($handle);

四、强制下载文件

$filename = $_GET['file']; //Get the fileid from the URL 
// Query the file ID 
$query = sprintf("SELECT * FROM tableName WHERE id = '%s'",mysql_real_escape_string($filename)); 
$sql = mysql_query($query); 
if(mysql_num_rows($sql) > 0){ 
$row = mysql_fetch_array($sql); 
// Set some headers 
header("Pragma: public"); 
header("Expires: 0"); 
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
header("Content-Type: application/force-download"); 
header("Content-Type: application/octet-stream"); 
header("Content-Type: application/download"); 
header("Content-Disposition: attachment; filename=".basename($row['FileName']).";"); 
header("Content-Transfer-Encoding: binary"); 
header("Content-Length: ".filesize($row['FileName'])); 
 
@readfile($row['FileName']); 
exit(0); 
}else{ 
header("Location: /"); 
exit; 
}

五、截取图片

$filename= "test.jpg"; 
list($w, $h, $type, $attr) = getimagesize($filename); 
$src_im = imagecreatefromjpeg($filename); 
 
$src_x = '0'; // begin x 
$src_y = '0'; // begin y 
$src_w = '100'; // width 
$src_h = '100'; // height 
$dst_x = '0'; // destination x 
$dst_y = '0'; // destination y 
 
$dst_im = imagecreatetruecolor($src_w, $src_h); 
$white = imagecolorallocate($dst_im, 255, 255, 255); 
imagefill($dst_im, 0, 0, $white); 
 
imagecopy($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h); 
 
header("Content-type: image/png"); 
imagepng($dst_im); 
imagedestroy($dst_im);

六、检查网站是否宕机

function Visit($url){ 
    $agent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";$ch=curl_init(); 
curl_setopt ($ch, CURLOPT_URL,$url ); 
curl_setopt($ch, CURLOPT_USERAGENT, $agent); 
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt ($ch,CURLOPT_VERBOSE,false); 
curl_setopt($ch, CURLOPT_TIMEOUT, 5); 
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, FALSE); 
curl_setopt($ch,CURLOPT_SSLVERSION,3); 
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, FALSE); 
$page=curl_exec($ch); 
//echo curl_error($ch); 
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
curl_close($ch); 
if($httpcode>=200 && $httpcode<300) return true; 
else return false; 
} 
if (Visit("http://www.google.com")) 
echo "Website OK"."n"; 
else 
echo "Website DOWN";

以上就是6个超实用的PHP代码样例,希望对大家学习PHP编程有所帮助,果断收藏吧

PHP 相关文章推荐
php Sql Server连接失败问题及解决办法
Aug 07 PHP
PHP简洁函数(PHP简单明了函数语法)
Jun 10 PHP
表格展示无限级分类(PHP版)
Aug 21 PHP
php中的注释、变量、数组、常量、函数应用介绍
Nov 16 PHP
php unset全局变量运用问题的深入解析
Jun 17 PHP
thinkphp的c方法使用示例
Feb 24 PHP
php中使用url传递数组的方法
Feb 11 PHP
PHP中的随机性 你觉得自己幸运吗?
Jan 22 PHP
关于Laravel Route重定向的一个注意点
Jan 16 PHP
php7 安装yar 生成docker镜像
May 09 PHP
PHP延迟静态绑定的深入讲解
Apr 02 PHP
php上传后台无法收到数据解决方法
Oct 28 PHP
解读PHP中的垃圾回收机制
Aug 10 #PHP
php实现多城市切换特效
Aug 09 #PHP
php基于双向循环队列实现历史记录的前进后退等功能
Aug 08 #PHP
PHP实现获取文件后缀名的几种常用方法
Aug 08 #PHP
PHP实现多维数组转字符串和多维数组转一维数组的方法
Aug 08 #PHP
Smarty使用自定义资源的方法
Aug 08 #PHP
SESSION存放在数据库用法实例
Aug 08 #PHP
You might like
PHP下一个非常全面获取图象信息的函数
2008/11/20 PHP
php解析html类库simple_html_dom(详细介绍)
2013/07/05 PHP
php 中phar包的使用教程详解
2018/10/26 PHP
两个SUBMIT按钮,如何区分处理
2006/08/22 Javascript
Javascript 鼠标移动上去 滑块跟随效果代码分享
2013/11/23 Javascript
jquery动态改变onclick属性导致失效的问题解决方法
2013/12/04 Javascript
浅谈JS闭包中的循环绑定处理程序
2014/11/09 Javascript
详解react-redux插件入门
2018/04/19 Javascript
JavaScript函数重载操作实例浅析
2020/05/02 Javascript
详解JavaScript 的执行机制
2020/09/18 Javascript
vue 导航守卫和axios拦截器有哪些区别
2020/12/19 Vue.js
python连接sql server乱码的解决方法
2013/01/28 Python
python使用rsa加密算法模块模拟新浪微博登录
2014/01/22 Python
python登陆asp网站页面的实现代码
2015/01/14 Python
在Python的Bottle框架中使用微信API的示例
2015/04/23 Python
浅谈python中scipy.misc.logsumexp函数的运用场景
2016/06/23 Python
python中异常报错处理方法汇总
2016/11/20 Python
在CentOS6上安装Python2.7的解决方法
2018/01/09 Python
Anaconda入门使用总结
2018/04/05 Python
VSCode下好用的Python插件及配置
2018/04/06 Python
selenium在执行phantomjs的API并获取执行结果的方法
2018/12/17 Python
在python下读取并展示raw格式的图片实例
2019/01/24 Python
Django多层嵌套ManyToMany字段ORM操作详解
2020/05/19 Python
python实现数字炸弹游戏
2020/07/17 Python
编写python代码实现简单抽奖器
2020/10/20 Python
HTML5本地存储之IndexedDB
2017/06/16 HTML / CSS
华为菲律宾官方网站:HUAWEI Philippines
2021/02/23 全球购物
如何在Shell脚本中使用函数
2015/09/06 面试题
幼师专业毕业生自荐信
2013/09/29 职场文书
外贸学院会计专业应届生求职信
2013/11/14 职场文书
酒店执行总经理岗位职责
2013/12/15 职场文书
电气个人求职信范文
2014/02/04 职场文书
预备党员政审材料
2014/02/04 职场文书
副护士长竞聘演讲稿
2014/04/30 职场文书
Golang 切片(Slice)实现增删改查
2022/04/22 Golang
Spring Security动态权限的实现方法详解
2022/06/16 Java/Android