php語法紀錄
擷取某個符號以前的字串
substr($string , 0, strpos($string ,"T"));
取得某個符號後的字串
$string_example='http://pjchender.blogspot.tw/2015/06/hyread.html'; echo strrchr($string_example, "/") // 從尾巴開始搜尋'/'這個符號 // 輸出結果/hyread.html echo substr(strrchr($string_example, "/"), 1); // 去除掉'/'這個符號,所以從第一個字元開始擷取到最後 // 輸出結果hyread.html
addslashes 函數的功能是替字串的特殊字符增加反斜線效果,會有這樣的需求主要在於部份的特殊符號會造成資料庫的錯誤或資料被竊取
addslashes( $string );
現在時間
date("Y-m-d H:i:s")
date("Y/m/d H:i",strtotime($taiwan['modifydate']));更改時間格式
開啟錯誤提示
error_reporting(E_ALL & ~E_NOTICE); ini_set('display_errors',1);
二維陣列排序
//後方的 > 可以改為 < usort(二維陣列, function($a, $b) { $al = $a['sort']; $bl = $b['sort']; if ($al == $bl)return 0; return ($al < $bl) ? -1 : 1;} );
二維陣列排序兩次
usort($data['all'], function($a, $b) { $criteria = array( 'win'=>'desc', 'score'=>'desc' //这里还可以根据需要继续加条件 如:'x'=>'asc'等 ); foreach($criteria as $what => $order){ if($a[$what] == $b[$what]){ continue; } return (($order == 'desc')?-1:1) * (($a[$what] < $b[$what]) ? -1 : 1); } });
驗證信箱
<?php
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $_POST["email"])) {
echo "Wrong Email Format!";
}
?>
檢查URL格式
1 2 3 4 5 | <?php if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i", $_POST['url'])) { echo "Wrong URL"; } ?> |
檔案下載
<?php
$file_name=$_GET['file_name']; //獲取檔名
header('content-disposition:attachment;filename='.$file_name); //告訴瀏覽器通過何種方式處理檔案
header('content-length:'.filesize($file_name)); //下載檔案的大小
readfile($file_name); //讀取檔案
?>
篩選 陣列0以上的有幾個
$array = array(20, 0, 0, 0, 0, 0, 0);
$temp = array_filter($array, function($value){
return $value > 0;
});
echo count($temp) >= 2 ? "true" : "false";
留言
張貼留言