Web开发者必备的10个救命的PHP代码片段
リリース: 2016-06-13 11:14:17
[代码] 关键词高亮
-
<span><span class="keyword">function</span><span> highlight(</span><span class="vars">$sString</span><span>, </span><span class="vars">$aWords</span><span>) { </span></span>
ログイン後にコピー
-
if (!is_array ($aWords) || emptyempty ($aWords) || !is_string ($sString)) {
-
return false;
- }
-
-
$sWords = implode ('|', $aWords);
-
return preg_replace ('@b('.$sWords.')b@si', '$1', $sString);
- }
[代码] 获取你的Feedburner的用户
-
<span><span class="keyword">function</span><span> get_average_readers(</span><span class="vars">$feed_id</span><span>,</span><span class="vars">$interval</span><span> = 7){ </span></span>
ログイン後にコピー
-
$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);
- }
[代码] 自动生成密码
-
<span><span class="keyword">function</span><span> generatePassword(</span><span class="vars">$length</span><span>=9, </span><span class="vars">$strength</span><span>=0) { </span></span>
ログイン後にコピー
-
$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文件
-
<span><span>header(</span><span class="string">'Content-type: text/css'</span><span>); </span></span>
ログイン後にコピー
-
ob_start("compress");
-
function compress($buffer) {
-
-
$buffer = preg_replace('!/*[^*]**+([^/][^*]**+)*/!', '', $buffer);
-
-
$buffer = str_replace(array("rn", "r", "n", "t", ' ', ' ', ' '), '', $buffer);
-
return $buffer;
- }
-
-
-
include('master.css');
-
include('typography.css');
-
include('grid.css');
-
include('print.css');
-
include('handheld.css');
-
- ob_end_flush();
[代码] 获取短网址
-
<span><span class="keyword">function</span><span> getTinyUrl(</span><span class="vars">$url</span><span>) { </span></span>
ログイン後にコピー
-
return file_get_contents("http://tinyurl.com/api-create.php?url=".$url);
- }
[代码] 根据生日计算年龄
-
<span><span class="keyword">function</span><span> age(</span><span class="vars">$date</span><span>){ </span></span>
ログイン後にコピー
-
$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 $month_diff $year_diff–;
-
-
return $year_diff;
- }
[代码] 计算执行时间
-
<span><span class="comment">//Create a variable for start time </span><span> </span></span>
ログイン後にコピー
-
$time_start = microtime(true);
-
-
-
-
-
$time_end = microtime(true);
-
-
$time = $time_end - $time_start;
-
-
echo 'Script took '.$time.' seconds to execute';
[代码] PHP的维护模式
-
<span><span class="keyword">function</span><span> maintenance(</span><span class="vars">$mode</span><span> = FALSE){ </span></span>
ログイン後にコピー
-
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样式被缓存
-
<span><span><link span="">
<span class="string">"/stylesheet.css?<!--?php echo time(); ?-->"</span><span> rel=</span><span class="string">"stylesheet"</span><span> type=</span><span class="string">"text/css"</span><span> /&glt; </span> href=</span></span>
ログイン後にコピー
[代码] 为数字增加 stndrd 等
-
<span><span class="keyword">function</span><span> make_ranked(</span><span class="vars">$rank</span><span>) { </span></span>
ログイン後にコピー
-
$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;
- }
href=
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
著者別の最新記事
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31