ホームページ  >  記事  >  バックエンド開発  >  10 の実践的な PHP コード スニペット_PHP チュートリアル

10 の実践的な PHP コード スニペット_PHP チュートリアル

WBOY
WBOYオリジナル
2016-07-21 15:24:23886ブラウズ

キーワード ハイライト

コードをコピー コードは次のとおりです:

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', '<強いスタイル= "background-color: yellow">$1', $sString);
}

Feedburner ユーザーを取得します
コードをコピーします コードは次のとおりです:

function get_average_readers($ feed_id,$interval = 7){
$today = date('Y-m-d', strtotime("now"));
$ago = date('Y-m-d', strtotime("-".$)間隔。" 日"));
$feed_url="https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=".$feed_id."&dates=".$ago.","。 $today;
$ch =curl_init();
curl_setopt($ch, CURLOPT_URL, $feed_url); ;
$ xml = 新しい SimpleXMLElement($data);
$fb = $xml->feed->entry['circulation'];
$nb = 0; Children() as $circ){
$nb += $circ['circulation']
}
returnround($nb/$interval)
}


パスワードを自動生成


コードは次のとおりです。
function generatedPassword($length=9, $strength=0) { $vowels = 'aeuy'; $consonants = 'bdghjmnpqrstvz'; if ($strength >= 1) {
$子音 .= ' BDGHJLMNPQRSTVWXZ';
}
if ($strength >= 2) {
$母音 .= "AEUY"
}
if ($strength >= 4) {
$子音 .= ' 23456789';
} if ($strength >= 8 ) {
$vowels .= '@#$%';
​​}
$alt = time() % 2; $i = 0; $i < $i++) {
if ($alt == 1) {
$password .= $consonants[(rand() % strlen($consonants))]; = 0;
} else {
$password .= $vowels[(rand() % strlen($vowels))];
$alt = 1;
}
return $password;


CSS ファイル



コードをコピーします

コードは次のとおりです:


header('Content-type: text/css');
function compress($buffer); /* コメントを削除 */
$buffer = preg_replace('!/*[^*]**+([^/][^*]**+)*/!', '', $buffer); * タブ、スペース、改行などを削除します。 */
$buffer = str_replace(array("rn", "r", "n", "t", ' ', ' ', ' '), '', $バッファ);
return $buffer;
/* CSS ファイル */ include('typography.css'); ('print.css'); include('handheld.css');
短縮URLを取得します



コードは次のとおりです:


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) –;
return $year_diff
}

実行時間を計算する コードをコピーする
コードは次のとおりです:


//開始時間の変数を作成します
$time_start = microtime(true);
// ここに PHP/HTML/JavaScript/CSS などを配置します
// 終了時刻の変数を作成します
$time_end = microtime(true)
// 2 つの時間を減算して秒を取得します
$time = $time_end; - $time_start;
echo 'スクリプトの実行に '.$time.' 秒かかりました'


PHP メンテナンス モード


コードは次のとおりです:

関数メンテナンス($mode = FALSE){
if($mode){
if(basename($_SERVER['SCRIPT_FILENAME']) != 'maintenance.php'){
header("場所: http:// example.com/maintenance.php");
exit;
}
}else{
if(basename($_SERVER['SCRIPT_FILENAME']) == 'maintenance.php'){
header("場所: http:/ /example.com/");
exit;
}
}
}

CSS スタイルがキャッシュされないようにします
コードをコピーします コードは次のとおりです:


番号などに stndrd を追加します
コード コードは次のとおりです。

function make_ranked($rank) {
$last = substr( $rank, -1 );
$seclast = substr( $rank, -2, -1 ); ( $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'; ) $ext = 'th';
if( $last == 3 && $seclast == 1) $ext = 'th'
return $rank.$ext;



http://www.bkjia.com/PHPjc/324314.html
www.bkjia.com

tru​​ehttp://www.bkjia.com/PHPjc/324314.html技術記事キーワードを強調表示するコピー コードは次のとおりです。 function highlight($sString, $aWords) { if (!is_array ($aWords) || empty ($aWords) || !is_string ($sString)) { return false } $sWords = 私は...
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。