10个实用的PHP代码片段


Posted in PHP onSeptember 02, 2011

关键词高亮

function highlight($sString, $aWords) { 
if (!is_array ($aWords) || empty ($aWords) || !is_string ($sString)) { 
return false; 
} 
$sWords = implode ('|', $aWords); 
return preg_replace ('@\b('.$sWords.')\b@si', '<strong style="background-color:yellow">$1</strong>', $sString); 
}

获取你的Feedburner的用户
function get_average_readers($feed_id,$interval = 7){ 
$today = date('Y-m-d', strtotime("now")); 
$ago = date('Y-m-d', strtotime("-".$interval." days")); 
$feed_url="https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=".$feed_id."&dates=".$ago.",".$today; 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_URL, $feed_url); 
$data = curl_exec($ch); 
curl_close($ch); 
$xml = new SimpleXMLElement($data); 
$fb = $xml->feed->entry['circulation']; 
$nb = 0; 
foreach($xml->feed->children() as $circ){ 
$nb += $circ['circulation']; 
} 
return round($nb/$interval); 
}

自动生成密码
function generatePassword($length=9, $strength=0) { 
$vowels = 'aeuy'; 
$consonants = 'bdghjmnpqrstvz'; 
if ($strength >= 1) { 
$consonants .= 'BDGHJLMNPQRSTVWXZ'; 
} 
if ($strength >= 2) { 
$vowels .= "AEUY"; 
} 
if ($strength >= 4) { 
$consonants .= '23456789'; 
} 
if ($strength >= 8 ) { 
$vowels .= '@#$%'; 
} 
$password = ''; 
$alt = time() % 2; 
for ($i = 0; $i < $length; $i++) { 
if ($alt == 1) { 
$password .= $consonants[(rand() % strlen($consonants))]; 
$alt = 0; 
} else { 
$password .= $vowels[(rand() % strlen($vowels))]; 
$alt = 1; 
} 
} 
return $password; 
}

压缩多个CSS文件
header('Content-type: text/css'); 
ob_start("compress"); 
function compress($buffer) { 
/* remove comments */ 
$buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer); 
/* remove tabs, spaces, newlines, etc. */ 
$buffer = str_replace(array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), '', $buffer); 
return $buffer; 
} 
/* your css files */ 
include('master.css'); 
include('typography.css'); 
include('grid.css'); 
include('print.css'); 
include('handheld.css'); 
ob_end_flush();

获取短网址
function getTinyUrl($url) { 
return file_get_contents("http://tinyurl.com/api-create.php?url=".$url); 
}

根据生日计算年龄
function age($date){ 
$year_diff = ''; 
$time = strtotime($date); 
if(FALSE === $time){ 
return ''; 
} 
$date = date('Y-m-d', $time); 
list($year,$month,$day) = explode("-",$date); 
$year_diff = date("Y") ? $year; 
$month_diff = date("m") ? $month; 
$day_diff = date("d") ? $day; 
if ($day_diff < 0 || $month_diff < 0) $year_diff?; 
return $year_diff; 
}

计算执行时间
//Create a variable for start time 
$time_start = microtime(true); 
// Place your PHP/HTML/JavaScript/CSS/Etc. Here 
//Create a variable for end time 
$time_end = microtime(true); 
//Subtract the two times to get seconds 
$time = $time_end - $time_start; 
echo 'Script took '.$time.' seconds to execute';

PHP的维护模式
function maintenance($mode = FALSE){ 
if($mode){ 
if(basename($_SERVER['SCRIPT_FILENAME']) != 'maintenance.php'){ 
header("Location: http://example.com/maintenance.php"); 
exit; 
} 
}else{ 
if(basename($_SERVER['SCRIPT_FILENAME']) == 'maintenance.php'){ 
header("Location: http://example.com/"); 
exit; 
} 
} 
}

阻止CSS样式被缓存
<link href="/stylesheet.css?<?php echo time(); ?>" rel="stylesheet" type="text/css" /&glt;

为数字增加 st\nd\rd 等
function make_ranked($rank) { 
$last = substr( $rank, -1 ); 
$seclast = substr( $rank, -2, -1 ); 
if( $last > 3 || $last == 0 ) $ext = 'th'; 
else if( $last == 3 ) $ext = 'rd'; 
else if( $last == 2 ) $ext = 'nd'; 
else $ext = 'st'; 
if( $last == 1 && $seclast == 1) $ext = 'th'; 
if( $last == 2 && $seclast == 1) $ext = 'th'; 
if( $last == 3 && $seclast == 1) $ext = 'th'; 
return $rank.$ext; 
}
PHP 相关文章推荐
PHP实现文件安全下载
Oct 09 PHP
让你的WINDOWS同时支持MYSQL4,MYSQL4.1,MYSQL5X
Dec 06 PHP
PHP 面向对象程序设计(oop)学习笔记(三) - 单例模式和工厂模式
Jun 12 PHP
jquery+php+ajax显示上传进度的多图片上传并生成缩略图代码
Oct 15 PHP
详解WordPress中提醒安装插件以及隐藏插件的功能实现
Dec 25 PHP
浅谈PHP中静态方法和非静态方法的相互调用
Oct 04 PHP
PHP Class SoapClient not found解决方法
Jan 20 PHP
PHP排序算法之基数排序(Radix Sort)实例详解
Apr 21 PHP
php微信公众号开发之微信企业付款给个人
Oct 04 PHP
PHP判断当前使用的是什么浏览器(推荐)
Oct 27 PHP
php使用pthreads v3多线程实现抓取新浪新闻信息操作示例
Feb 21 PHP
php设计模式之组合模式实例详解【星际争霸游戏案例】
Mar 27 PHP
PHP文件操作实现代码分享
Sep 01 #PHP
深入探讨PHP中的内存管理问题
Aug 31 #PHP
php中使用Imagick实现图像直方图的实现代码
Aug 30 #PHP
PHP正确配置mysql(apache环境)
Aug 28 #PHP
PHP MySQL应用中使用XOR运算加密算法分享
Aug 28 #PHP
PHP 时间日期操作实战
Aug 26 #PHP
PHP url 加密解密函数代码
Aug 26 #PHP
You might like
php disk_free_space 返回目录可用空间
2010/05/10 PHP
PHP随机字符串生成代码(包括大小写字母)
2013/06/24 PHP
Yii2主题(Theme)用法详解
2016/07/23 PHP
PHP+Mysql+Ajax实现淘宝客服或阿里旺旺聊天功能(前台页面)
2017/06/16 PHP
php处理多图上传压缩代码功能
2018/06/13 PHP
PHP删除数组中指定值的元素常用方法实例分析【4种方法】
2018/08/21 PHP
jquery 获取json数据实现代码
2009/04/27 Javascript
选择TreeView控件的树状数据节点的JS方法(jquery)
2010/02/06 Javascript
JS 对输入框进行限制(常用的都有)
2013/07/30 Javascript
jQuery设置div一直在页面顶部显示的方法
2013/10/24 Javascript
jQuery层级选择器用法分析
2015/02/10 Javascript
javaScript中push函数用法实例分析
2015/06/08 Javascript
AngularJS基础 ng-disabled 指令详解及简单示例
2016/08/01 Javascript
Bootstrap栅格系统的使用和理解2
2016/12/14 Javascript
微信小程序实现基于三元运算验证手机号/姓名功能示例
2019/01/19 Javascript
Vue 实现简易多行滚动&quot;弹幕&quot;效果
2020/01/02 Javascript
vue中全局路由守卫中替代this操作(this.$store/this.$vux)
2020/07/24 Javascript
js实现手表表盘时钟与圆周运动
2020/09/18 Javascript
vue绑定class的三种方法
2020/12/24 Vue.js
Python with语句上下文管理器两种实现方法分析
2018/02/09 Python
python如何修改装饰器中参数
2018/03/20 Python
Django框架模板介绍
2019/01/15 Python
Python基于opencv实现的简单画板功能示例
2019/03/04 Python
python 字典套字典或列表的示例
2019/12/16 Python
记一次django内存异常排查及解决方法
2020/08/07 Python
python使用matplotlib:subplot绘制多个子图的示例
2020/09/24 Python
室内设计专业个人的自我评价
2013/10/19 职场文书
大学生职业生涯设计书
2014/01/02 职场文书
学雷锋标语
2014/06/25 职场文书
大学生考试作弊检讨书
2014/09/21 职场文书
2015大学生自我评价范文
2015/03/03 职场文书
2016年植树节红领巾广播稿
2015/12/17 职场文书
2016年3月份红领巾广播稿
2015/12/21 职场文书
有关花店创业的计划书模板
2019/08/27 职场文书
nginx对http请求处理的各个阶段详析
2021/03/31 Servers
MySQL 1130异常,无法远程登录解决方案详解
2021/08/23 MySQL