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 相关文章推荐
windows下PHP APACHE MYSQ完整配置
Jan 02 PHP
PHP运行时强制显示出错信息的代码
Apr 20 PHP
仿Aspnetpager的一个PHP分页类代码 附源码下载
Oct 08 PHP
PHP跨时区(UTC时间)应用解决方案
Jan 11 PHP
深入PHP内存相关的功能特性详解
Jun 08 PHP
基于PHP5魔术常量与魔术方法的详解
Jun 13 PHP
浅析PKI加密解密 OpenSSL
Jul 01 PHP
php实现mysql数据库操作类分享
Feb 14 PHP
php中magic_quotes_gpc对unserialize的影响分析
Dec 16 PHP
Yii框架中使用PHPExcel的方法分析
Jul 25 PHP
php 下 html5 XHR2 + FormData + File API 上传文件操作实例分析
Feb 28 PHP
php+mysql+ajax 局部刷新点赞/取消点赞功能(每个账号只点赞一次)
Jul 24 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
消息持续发送的完整例子
2006/10/09 PHP
php结合表单实现一些简单功能的例子
2011/06/04 PHP
smarty模板局部缓存方法使用示例
2014/06/17 PHP
iis6手工创建网站后无法运行php脚本的解决方法
2017/06/08 PHP
Yii2 中实现单点登录的方法
2018/03/09 PHP
利用jquery包将字符串生成二维码图片
2013/09/12 Javascript
原生js实现复制对象、扩展对象 类似jquery中的extend()方法
2014/08/30 Javascript
推荐10 款 SVG 动画的 JavaScript 库
2015/03/24 Javascript
angularjs学习笔记之双向数据绑定
2015/09/26 Javascript
js实现创建删除html元素小结
2015/09/30 Javascript
JS实现带提示的星级评分效果完整实例
2015/10/30 Javascript
原生JS和jQuery操作DOM对比总结
2017/01/19 Javascript
Vue的土著指令和自定义指令实例详解
2018/02/04 Javascript
Vue 仿QQ左滑删除组件功能
2018/03/12 Javascript
详解Vue This$Store总结
2018/12/17 Javascript
微信小程序自定义带价格显示日历效果
2018/12/29 Javascript
js实现简单的日历显示效果函数示例
2019/11/25 Javascript
JavaScript实现好看的跟随彩色气泡效果
2020/02/06 Javascript
分数霸榜! python助你微信跳一跳拿高分
2018/01/08 Python
scikit-learn线性回归,多元回归,多项式回归的实现
2019/08/29 Python
python获取引用对象的个数方式
2019/12/20 Python
Pycharm无法打开双击没反应的问题及解决方案
2020/08/17 Python
Python经纬度坐标转换为距离及角度的实现
2020/11/01 Python
CSS3轻松实现清新 Loading 效果的简单实例
2016/06/06 HTML / CSS
html5-websocket基于远程方法调用的数据交互实现
2012/12/04 HTML / CSS
京东国际站:JOYBUY
2017/11/23 全球购物
获取邓白氏信用报告:Dun & Bradstreet
2019/01/22 全球购物
美国领先的机场停车聚合商:Airport Parking Reservations
2020/02/28 全球购物
护士实习自我鉴定
2013/10/22 职场文书
小学家长会邀请函
2014/01/23 职场文书
投标邀请书范文
2014/01/31 职场文书
逃课检讨书怎么写
2015/01/01 职场文书
高中美术教学反思
2016/02/17 职场文书
python numpy中setdiff1d的用法说明
2021/04/22 Python
一文带你理解vue创建一个后台管理系统流程(Vue+Element)
2021/05/18 Vue.js
CSS 左边固定宽右边自适应的6种方法
2022/05/15 HTML / CSS