[代码] 关键词高亮
<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 ) || empty empty ( $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=
http://www.bkjia.com/PHPjc/445722.html www.bkjia.com true http://www.bkjia.com/PHPjc/445722.html TechArticle [代码] 关键词高亮 function highlight( $sString , $aWords ){ if (! is_array ( $aWords )|| empty empty ( $aWords )||! is_string ( $sString )){ return false; } $sWords =implode...
Déclaration: Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn