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 相关文章推荐
应用开发中涉及到的css和php笔记分享
Aug 02 PHP
php eval函数用法 PHP中eval()函数小技巧
Oct 31 PHP
Zend的Registry机制的使用说明
May 02 PHP
wamp下修改mysql访问密码的解决方法
May 07 PHP
PHP使用xmllint命令处理xml与html的方法
Dec 15 PHP
php编写的一个E-mail验证类
Mar 25 PHP
PHP开启opcache提升代码性能
Apr 26 PHP
php数组索引与键值操作技巧实例分析
Jun 24 PHP
PHP编写学校网站上新生注册登陆程序的实例分享
Mar 21 PHP
YII框架中使用memcache的方法详解
Aug 02 PHP
Thinkphp5框架实现获取数据库数据到视图的方法
Aug 14 PHP
php去除数组中为0的元素的实例分析
Nov 17 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
百度实时推送api接口应用示例
2014/10/21 PHP
PHP新特性之字节码缓存和内置服务器
2017/08/11 PHP
Javascript this指针
2009/07/30 Javascript
Javascript事件热键兼容ie|firefox
2010/12/30 Javascript
JS实现点击下载的小例子
2013/07/10 Javascript
限制textbox或textarea输入字符长度的JS代码
2013/10/16 Javascript
JavaScript对象属性检查、增加、删除、访问操作实例
2015/07/08 Javascript
javascript实现的登陆遮罩效果汇总
2015/11/09 Javascript
jQuery插件实现多级联动菜单效果
2015/12/01 Javascript
JavaScript+html5 canvas绘制缤纷多彩的三角形效果完整实例
2016/01/26 Javascript
jquery插件jquery.LightBox.js实现点击放大图片并左右点击切换效果(附demo源码下载)
2016/02/25 Javascript
JS得到当前时间的方法示例
2017/03/24 Javascript
jQuery表单设置值的方法
2017/06/30 jQuery
jQuery操作DOM_动力节点Java学院整理
2017/07/04 jQuery
React Native 环境搭建的教程
2017/08/19 Javascript
vue项目创建并引入饿了么elementUI组件的步骤
2019/04/11 Javascript
js实现整体缩放页面适配移动端
2020/03/31 Javascript
Ubuntu 下 vim 搭建python 环境 配置
2017/06/12 Python
Python将DataFrame的某一列作为index的方法
2018/04/08 Python
解决python3中的requests解析中文页面出现乱码问题
2019/04/19 Python
numpy:np.newaxis 实现将行向量转换成列向量
2019/11/30 Python
python时间与Unix时间戳相互转换方法详解
2020/02/13 Python
python使用Geany编辑器配置方法
2020/02/21 Python
Python使用matplotlib绘制圆形代码实例
2020/05/27 Python
Python numpy矩阵处理运算工具用法汇总
2020/07/13 Python
Canvas中设置width与height的问题浅析
2018/11/01 HTML / CSS
"引用"与多态的关系
2013/02/01 面试题
自我评价范文
2013/12/22 职场文书
卫生巾广告词
2014/03/18 职场文书
初二学生评语大全
2014/12/26 职场文书
2016年3月份红领巾广播稿
2015/12/21 职场文书
CSS3实现的3D隧道效果
2021/04/27 HTML / CSS
JavaScript分页组件使用方法详解
2021/07/26 Javascript
浅谈Redis的keys命令到底有多慢
2021/10/05 Redis
MySQL数据库之内置函数和自定义函数 function
2022/06/16 MySQL
windows11选中自动复制怎么开启? Win11自动复制所选内容的方法
2022/07/23 数码科技