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中通过ADODB库实现调用Access数据库之修正版本
Dec 31 PHP
用PHP实现的生成静态HTML速度快类库
Mar 31 PHP
php str_pad() 将字符串填充成指定长度的字符串
Feb 23 PHP
PHP 强制性文件下载功能的函数代码(任意文件格式)
May 26 PHP
PHP文件读写操作之文件写入代码
Jan 13 PHP
openPNE常用方法分享
Nov 29 PHP
PHP四种基本排序算法示例
Apr 09 PHP
搭建自己的PHP MVC框架详解
Aug 16 PHP
PHP保留两位小数的几种方法
Jul 24 PHP
实现laravel 插入操作日志到数据库的方法
Oct 11 PHP
解决在laravel中leftjoin带条件查询没有返回右表为NULL的问题
Oct 15 PHP
php设计模式之抽象工厂模式分析【星际争霸游戏案例】
Jan 23 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面向对象全攻略 (十六) 对象的串行化
2009/09/30 PHP
分享8个最佳的代码片段在线测试网站
2013/06/29 PHP
使用Discuz关键词服务器实现PHP中文分词
2014/03/11 PHP
ThinkPHP5.0框架验证码功能实现方法【基于第三方扩展包】
2019/03/11 PHP
仅IE不支持setTimeout/setInterval函数的第三个以上参数
2011/05/25 Javascript
javascript中的缓动效果实现程序
2012/12/29 Javascript
关于JavaScript中string 的replace
2013/04/12 Javascript
JS控制日期显示的小例子
2013/11/23 Javascript
调用DOM对象的focus使文本框获得焦点
2014/02/19 Javascript
Extjs表单常见验证小结
2014/03/07 Javascript
jquery ajax应用中iframe自适应高度问题解决方法
2014/04/12 Javascript
javascript无刷新评论实现方法
2015/05/13 Javascript
javascript中使用new与不使用实例化对象的区别
2015/06/22 Javascript
javaScript中定义类或对象的五种方式总结
2016/12/04 Javascript
Bootstrap基本插件学习笔记之轮播幻灯片(23)
2016/12/08 Javascript
老生常谈的跨域处理
2017/01/11 Javascript
React Native验证码倒计时工具类分享
2017/10/24 Javascript
在knockoutjs 上自己实现的flux(实例讲解)
2017/12/18 Javascript
详解使用create-react-app添加css modules、sasss和antd
2018/07/31 Javascript
js节流防抖应用场景,以及在vue中节流防抖的具体实现操作
2020/09/21 Javascript
nodeJs项目在阿里云的简单部署
2020/11/27 NodeJs
python实现简单登陆系统
2018/10/18 Python
pytorch 调整某一维度数据顺序的方法
2018/12/08 Python
利用python计算windows全盘文件md5值的脚本
2019/07/27 Python
Python写出新冠状病毒确诊人数地图的方法
2020/02/12 Python
numpy矩阵数值太多不能全部显示的解决
2020/05/14 Python
python 中的命名空间,你真的了解吗?
2020/08/19 Python
澳大利亚的奢侈品牌:Oroton
2016/08/26 全球购物
世界最大的海报和艺术印刷商店:AllPosters.com
2017/02/01 全球购物
财务主管自我鉴定
2014/01/17 职场文书
毕业自我鉴定总结
2014/03/24 职场文书
学校爱国卫生月活动总结
2014/06/25 职场文书
公务员群众路线专题民主生活会发言材料
2014/09/17 职场文书
个性与发展自我评价
2015/03/06 职场文书
谁动了我的奶酪读书笔记
2015/06/30 职场文书
2016年七夕情人节宣传语
2015/11/25 职场文书