国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

10個(gè)實(shí)用的PHP代碼片段

2024-05-04 23:16:57
字體:
供稿:網(wǎng)友
關(guān)鍵詞高亮

復(fù)制代碼 代碼如下:


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>$1</strong>', $sString);
}


獲取你的Feedburner的用戶

復(fù)制代碼 代碼如下:


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);
}


自動(dòng)生成密碼

復(fù)制代碼 代碼如下:


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;
}


壓縮多個(gè)CSS文件

復(fù)制代碼 代碼如下:


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();


獲取短網(wǎng)址

復(fù)制代碼 代碼如下:


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


根據(jù)生日計(jì)算年齡

復(fù)制代碼 代碼如下:


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;
}


計(jì)算執(zhí)行時(shí)間

復(fù)制代碼 代碼如下:


//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的維護(hù)模式

復(fù)制代碼 代碼如下:


function maintenance($mode = FALSE){
if($mode){
if(basename($_SERVER['SCRIPT_FILENAME']) != 'maintenance.php'){
header("Location: ");
exit;
}
}else{
if(basename($_SERVER['SCRIPT_FILENAME']) == 'maintenance.php'){
header("Location: ");
exit;
}
}
}


阻止CSS樣式被緩存

復(fù)制代碼 代碼如下:


<link href="/stylesheet.css?<?php echo time(); ?>" type="text/css" /&glt;


為數(shù)字增加 st/nd/rd 等

復(fù)制代碼 代碼如下:


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;
}

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 祁门县| 迁西县| 庄河市| 互助| 神木县| 类乌齐县| 曲阳县| 文安县| 思南县| 阿拉尔市| 武强县| 龙游县| 凤庆县| 巴楚县| 金乡县| 深圳市| 大关县| 虹口区| 栾城县| 万宁市| 泽普县| 龙海市| 泸定县| 托克托县| 丽江市| 龙州县| 孟村| 胶南市| 安陆市| 皋兰县| 田阳县| 南投县| 碌曲县| 仁化县| 荆门市| 平南县| 中超| 关岭| 崇文区| 太原市| 潼关县|