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 相关文章推荐
一个ubbcode的函数,速度很快.
Oct 09 PHP
AJAX for PHP简单表数据查询实例
Jan 02 PHP
实例(Smarty+FCKeditor新闻系统)
Jan 02 PHP
php实现的简单压缩英文字符串的代码
Apr 24 PHP
Cakephp 执行主要流程
Mar 24 PHP
php生成图形(Libchart)实例
Nov 06 PHP
重新认识php array_merge函数
Aug 31 PHP
微信公众平台之快递查询功能用法实例
Apr 14 PHP
如何使用php脚本给html中引用的js和css路径打上版本号
Nov 18 PHP
php+ajax注册实时验证功能
Jul 20 PHP
php 无限分类 树形数据格式化代码
Oct 11 PHP
通过PHP设置BugFree获取邮箱通知
Apr 25 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 多维数组排序实现代码
2009/08/05 PHP
phpcms实现验证码替换及phpcms实现全站搜索功能教程详解
2017/12/13 PHP
PHP中的自动加载操作实现方法详解
2019/08/06 PHP
javascript操作excel生成报表全攻略
2014/05/04 Javascript
JQuery中DOM加载与事件执行实例分析
2015/06/13 Javascript
AngularJS 避繁就简的路由
2016/07/01 Javascript
js验证真实姓名与身份证号,手机号的简单实例
2016/07/18 Javascript
js实现前端分页页码管理
2017/01/06 Javascript
JS 实现计算器详解及实例代码(一)
2017/01/08 Javascript
详解angularjs中的隔离作用域理解以及绑定策略
2017/05/31 Javascript
AngularJS 教程及实例代码
2017/10/23 Javascript
Angular实现下拉框模糊查询功能示例
2018/01/03 Javascript
webpack项目轻松混用css module的方法
2018/06/12 Javascript
对node通过fs模块判断文件是否是文件夹的实例讲解
2019/06/10 Javascript
vue+axios 拦截器实现统一token的案例
2020/09/11 Javascript
微信小程序实现选项卡滑动切换
2020/10/22 Javascript
详解Python pygame安装过程笔记
2017/06/05 Python
对python3 中方法各种参数和返回值详解
2018/12/15 Python
python根据txt文本批量创建文件夹
2020/12/08 Python
pandas实现to_sql将DataFrame保存到数据库中
2019/07/03 Python
django如何实现视图重定向
2019/07/24 Python
Python Django Vue 项目创建过程详解
2019/07/29 Python
Python爬取豆瓣视频信息代码实例
2019/11/16 Python
基于python计算滚动方差(标准差)talib和pd.rolling函数差异详解
2020/06/08 Python
Python如何在bool函数中取值
2020/09/21 Python
python 牛顿法实现逻辑回归(Logistic Regression)
2020/10/15 Python
CSS3制作精致的照片墙特效
2016/06/07 HTML / CSS
CSS3实现精美横向滚动菜单按钮
2017/04/14 HTML / CSS
html5实现多文件的上传示例代码
2014/02/13 HTML / CSS
TUMI澳大利亚网站:美国旅行箱包品牌
2017/03/27 全球购物
动物学专业毕业生求职信
2013/10/11 职场文书
汽车维修专业毕业生的求职信分享
2013/12/04 职场文书
改进工作作风心得体会
2016/01/23 职场文书
什么是创业计划书?什么是商业计划书?这里一一解答
2019/07/12 职场文书
解决flex布局中子项目尺寸不受flex-shrink限制
2022/05/11 HTML / CSS
Android开发手册TextInputLayout样式使用示例
2022/06/10 Java/Android