10个实用的PHP代码片段

前端之家收集整理的这篇文章主要介绍了10个实用的PHP代码片段前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

关键词高亮
<div class="codetitle"><a style="CURSOR: pointer" data="21302" class="copybut" id="copybut21302" onclick="doCopy('code21302')"> 代码如下:

<div class="codebody" id="code21302">
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','<h3 style="background-color:yellow">$1',$sString);
}

获取你的Feedburner的用户
<div class="codetitle"><a style="CURSOR: pointer" data="13016" class="copybut" id="copybut13016" onclick="doCopy('code13016')"> 代码如下:
<div class="codebody" id="code13016">
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);
}

自动生成密码
<div class="codetitle"><a style="CURSOR: pointer" data="90990" class="copybut" id="copybut90990" onclick="doCopy('code90990')"> 代码如下:
<div class="codebody" id="code90990">
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文件
<div class="codetitle"><a style="CURSOR: pointer" data="30511" class="copybut" id="copybut30511" onclick="doCopy('code30511')"> 代码如下:
<div class="codebody" id="code30511">
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();

获取短网址
<div class="codetitle"><a style="CURSOR: pointer" data="11948" class="copybut" id="copybut11948" onclick="doCopy('code11948')"> 代码如下:<div class="codebody" id="code11948">
function getTinyUrl($url) {
return file_get_contents("http://tinyurl.com/api-create.PHP?url=".$url);
}

根据生日计算年龄
<div class="codetitle"><a style="CURSOR: pointer" data="94194" class="copybut" id="copybut94194" onclick="doCopy('code94194')"> 代码如下:<div class="codebody" id="code94194">
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;
}

计算执行时间
<div class="codetitle"><a style="CURSOR: pointer" data="20897" class="copybut" id="copybut20897" onclick="doCopy('code20897')"> 代码如下:<div class="codebody" id="code20897">
//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的维护模式
<div class="codetitle"><a style="CURSOR: pointer" data="40787" class="copybut" id="copybut40787" onclick="doCopy('code40787')"> 代码如下:<div class="codebody" id="code40787">
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样式被缓存
<div class="codetitle"><a style="CURSOR: pointer" data="7675" class="copybut" id="copybut7675" onclick="doCopy('code7675')"> 代码如下:<div class="codebody" id="code7675">
<link href="/stylesheet.css?<?php echo time(); ?>" rel="stylesheet" type="text/css" /&glt;

为数字增加 st\nd\rd 等
<div class="codetitle"><a style="CURSOR: pointer" data="7012" class="copybut" id="copybut7012" onclick="doCopy('code7012')"> 代码如下:<div class="codebody" id="code7012">
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;
}

原文链接:https://www.f2er.com/php/27642.html

猜你在找的PHP相关文章