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 相关文章推荐
关于Iframe如何跨域访问Cookie和Session的解决方法
Apr 15 PHP
PHP扩展模块Pecl、Pear以及Perl的区别
Apr 09 PHP
开源php中文分词系统SCWS安装和使用实例
Apr 11 PHP
php数组键值用法实例分析
Feb 27 PHP
php实现粘贴截图并完成上传功能
May 17 PHP
php实现的Curl封装类Curl.class.php用法实例分析
Sep 25 PHP
几个优化WordPress中JavaScript加载体验的插件介绍
Dec 17 PHP
[原创]CI(CodeIgniter)简单统计访问人数实现方法
Jan 19 PHP
Zend Framework实现多文件上传功能实例
Mar 21 PHP
Zend Framework框架实现类似Google搜索分页效果
Nov 25 PHP
php 7新特性之类型申明详解
Jun 06 PHP
使用Git实现Laravel项目的自动化部署
Nov 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
php中将一段数据存到一个txt文件中并显示其内容
2014/08/15 PHP
PHP微信公众号开发之微信红包实现方法分析
2017/07/14 PHP
ThinkPHP5.0框架控制器继承基类和自定义类示例
2018/05/25 PHP
php实现微信小程序授权登录功能(实现流程)
2019/11/13 PHP
javascript 清除输入框中的数据
2009/04/13 Javascript
js中的事件捕捉模型与冒泡模型实例分析
2015/01/10 Javascript
JS实现文字向下滚动完整实例
2015/02/06 Javascript
JQuery中两个ul标签的li互相移动实现方法
2015/05/18 Javascript
JS动态添加iframe的代码
2015/09/14 Javascript
javascript实现瀑布流加载图片原理
2016/02/02 Javascript
浅析JS原型继承与类的继承
2016/04/07 Javascript
JavaScript  cookie 跨域访问之广告推广
2016/04/20 Javascript
JS 实现Base64编码与解码实例详解
2016/11/07 Javascript
bootstrap模态框消失问题的解决方法
2016/12/02 Javascript
javascript显示系统当前时间代码
2016/12/29 Javascript
bootstrap制作jsp页面(根据值让table显示选中)
2017/01/05 Javascript
vue实现图片加载完成前的loading组件方法
2018/02/05 Javascript
es6数据变更同步到视图层的方法
2019/03/04 Javascript
微信小程序实现原生步骤条
2019/07/25 Javascript
JS中的算法与数据结构之列表(List)实例详解
2019/08/16 Javascript
基于vue实现图片验证码倒计时60s功能
2019/12/10 Javascript
[01:07:19]2018DOTA2亚洲邀请赛 4.5 淘汰赛 Mineski vs VG 第一场
2018/04/06 DOTA
Python实现遍历数据库并获取key的值
2015/05/17 Python
Python爬虫使用Selenium+PhantomJS抓取Ajax和动态HTML内容
2018/02/23 Python
python+splinter自动刷新抢票功能
2018/09/25 Python
使用PyQt4 设置TextEdit背景的方法
2019/06/14 Python
keras多显卡训练方式
2020/06/10 Python
微信小程序实现可实时改变转速的css3旋转动画实例代码
2018/09/11 HTML / CSS
医学生自我鉴定范文
2013/11/08 职场文书
网站美工岗位职责
2014/04/02 职场文书
珍惜资源保护环境的建议书
2014/05/14 职场文书
群众路线教育实践活动实施方案
2014/10/31 职场文书
2015上半年个人工作总结
2015/07/27 职场文书
浪漫婚礼主持词开场白
2015/11/24 职场文书
Redis之RedisTemplate配置方式(序列和反序列化)
2022/03/13 Redis
英国数字版游戏销量周榜公布 《小缇娜的奇幻之地》登顶
2022/04/03 其他游戏