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 相关文章推荐
支持数组的ADDSLASHES的php函数
Feb 16 PHP
创建配置文件 用PHP写出自己的BLOG系统 2
Apr 12 PHP
利用php+mysql来做一个功能强大的在线计算器
Oct 12 PHP
php多维数组去掉重复值示例分享
Mar 02 PHP
PHP学习笔记(二) 了解PHP的基本语法以及目录结构
Aug 04 PHP
PHP中array_slice函数用法实例详解
Nov 25 PHP
php获取当前页面完整URL地址
Dec 30 PHP
详解YII关联查询
Jan 10 PHP
php实现计算百度地图坐标之间距离的方法
May 05 PHP
全面解析PHP验证码的实现原理 附php验证码小案例
Aug 17 PHP
PHP实现在windows下配置sendmail并通过mail()函数发送邮件的方法
Jun 20 PHP
Django中通过定时任务触发页面静态化的处理方式
Aug 29 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开发中四种查询返回结果分析
2011/01/02 PHP
关于PHP中Object对象的笔记分享
2011/06/28 PHP
php的ajax简单实例
2014/02/27 PHP
Thinkphp+smarty+uploadify实现无刷新上传
2015/07/30 PHP
详细解读php的命名空间(一)
2018/02/21 PHP
JavaScript 编程引入命名空间的方法
2007/06/29 Javascript
javascript 动态生成私有变量访问器
2009/12/06 Javascript
nodejs入门详解(多篇文章结合)
2012/03/07 NodeJs
jquery实现的一个导航滚动效果具体代码
2013/05/27 Javascript
详解JavaScript的策略模式编程
2015/06/24 Javascript
JavaScript中setter和getter方法介绍
2016/07/11 Javascript
利用JS轻松实现获取表单数据
2016/12/06 Javascript
jquery编写日期选择器
2017/03/16 Javascript
react-native ListView下拉刷新上拉加载实现代码
2017/08/03 Javascript
vue2.0 移动端实现下拉刷新和上拉加载更多的示例
2018/04/23 Javascript
angular的输入和输出的使用方法
2018/09/22 Javascript
微信小程序中为什么使用var that=this
2019/08/27 Javascript
python数据类型_元组、字典常用操作方法(介绍)
2017/05/30 Python
python使用__slots__让你的代码更加节省内存
2018/09/05 Python
Django中数据库的数据关系:一对一,一对多,多对多
2018/10/21 Python
DataFrame:通过SparkSql将scala类转为DataFrame的方法
2019/01/29 Python
Python单元测试模块doctest的具体使用
2020/02/10 Python
Python中random模块常用方法的使用教程
2020/10/04 Python
关于Python3的import问题(pycharm可以运行命令行import错误)
2020/11/18 Python
使用python画出逻辑斯蒂映射(logistic map)中的分叉图案例
2020/12/11 Python
求两个数的乘积和商数,该作用由宏定义来实现
2013/03/13 面试题
视光学专业毕业生推荐信
2013/10/28 职场文书
拆迁委托协议书
2014/09/15 职场文书
大学生第一学年自我鉴定2015
2014/09/28 职场文书
学习党的群众路线教育实践活动心得体会范文
2014/11/03 职场文书
教代会闭幕词
2015/01/28 职场文书
建筑技术负责人岗位职责
2015/04/13 职场文书
2015年驾驶员工作总结
2015/04/29 职场文书
python 开心网和豆瓣日记爬取的小爬虫
2021/05/29 Python
高性能跳频抗干扰宽带自组网电台
2022/02/18 无线电
Redis 报错 error:NOAUTH Authentication required
2022/05/15 Redis