防止恶意调用代码:在调用的页面使用:define('IN_TG", true); //定义一个常量,用来授权调用inclued,require里面的文件
在被调用的页面使用: if (!defined('IN_TG")) { exit('Access Denied'); }
转换硬路径常量:define('ROOT_PATH', substr(dirname(_FILE_),0,-8));
拒绝php低版本: if (PHP_VERSION< '6.1.0') {eixt('Version is too low'); }
程序执行耗时:1. //程序运行开始时间
$startTime = explode(' ', microtime());
/*········以下是代码区·········*/
for($i = 0; $i < 1000000; $i++){ $i;}
/*········以上是代码区·········*
///程序运行结束时间
$endTime = explode(' ', microtime());echo $startTime[0] + $startTime[1] . '<br/>';
echo $endTime[0] + $endTime[1] . '<br/>';
echo '执行耗时:' . round($endTime[0] + $endTime[1] - ($startTime[0] + $startTime[1]), 4) . ' 秒。';
2. $t1 = microtime(true);
// ... 执行代码 ...
$t2 = microtime(true);
echo '耗时'.round($t2-$t1,3).'秒<br>';
消耗的内存: echo 'Now memory_get_usage: ' . memory_get_usage() . '<br />';
// memory_get_usage() 返回的单位是b,/1024得到kb,/(1024*1024)得到mb,依次类推。
php分页: $start = ($curPage - 1) * $page;
$returnData = array_slice($result, $start, $page); 手机号脱敏:t = $tel.substr(0,3)+'****'+$tel.substr(7,4); 银行卡每四位一个空格:
if(strlen($bxAccount) <= 16){ preg_match('/([\d]{4})([\d]{4})([\d]{4})([\d]{0,})?/', $bxAccount,$match); } else if(strlen($bxAccount) >16 && strlen($bxAccount) <= 20) { preg_match('/([\d]{4})([\d]{4})([\d]{4})([\d]{4})([\d]{0,})?/', $bxAccount,$match); } else if(strlen($bxAccount) >20 && strlen($bxAccount) <25 ){ preg_match('/([\d]{4})([\d]{4})([\d]{4})([\d]{4})([\d]{4})([\d]{0,})?/', $bxAccount,$match); } else { preg_match('/([\d]{4})([\d]{4})([\d]{4})([\d]{4})([\d]{4})([\d]{4})([\d]{0,})?/', $bxAccount,$match); } unset($match[0]); $baiXin = implode(' ', $match); 统一规范返回信息: protected function responseInfo($status, $msg, $data = '') { $returnInfo = array( 'status' => $status, 'msg' => $msg, 'data' => $data ); header('Content-Type:application/json'); echo json_encode($returnInfo); return $returnInfo;
}
判断是否为空或0
public function issetEmpty($v) { if (isset($v) && $v === false) { return true; } if (isset($v) && ($v === 0 || $v == '0')) { return false; } if (!empty($v)) { return false; } return true; }
判断银行卡
if (!is_numeric($bankAccount) || !preg_match('/^\d{15,22}$/', $bankAccount)) { return $this->responseInfo(-9, '银行卡错误'); }