PHP常用的一些公共函式(持續更新)
阿新 • • 發佈:2019-02-11
<?php // 應用公共檔案 // =================================新加入===================================== function getIP() { static $realip; if (isset($_SERVER)){ if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])){ $realip = $_SERVER["HTTP_X_FORWARDED_FOR"]; } else if (isset($_SERVER["HTTP_CLIENT_IP"])) { $realip = $_SERVER["HTTP_CLIENT_IP"]; } else { $realip = $_SERVER["REMOTE_ADDR"]; } } else { if (getenv("HTTP_X_FORWARDED_FOR")){ $realip = getenv("HTTP_X_FORWARDED_FOR"); } else if (getenv("HTTP_CLIENT_IP")) { $realip = getenv("HTTP_CLIENT_IP"); } else { $realip = getenv("REMOTE_ADDR"); } } return $realip; } function getCity($ip = '') { if($ip == ''){ $url = "http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json"; $ip=json_decode(file_get_contents($url),true); $data = $ip; }else{ $url="http://ip.taobao.com/service/getIpInfo.php?ip=".$ip; $ip=json_decode(file_get_contents($url)); if((string)$ip->code=='1'){ return false; } $data = (array)$ip->data; } return $data; } /*===================================================== * 說明:手機號 隱藏 by:Simon *====================================================*/ function mobile_phone_hidden($phone) { $is_what = preg_match('/(0[0-9]{2,3}[\-]?[2-9][0-9]{6,7}[\-]?[0-9]?)/i',$phone); //固定電話 if($is_what == 1) return preg_replace('/(0[0-9]{2,3}[\-]?[2-9])[0-9]{3,4}([0-9]{3}[\-]?[0-9]?)/i','$1****$2',$phone); else return preg_replace('/(1[358]{1}[0-9])[0-9]{4}([0-9]{4})/i','$1****$2',$phone); } /*===================================================== * 說明:身份證號 隱藏 by:Simon *====================================================*/ function id_card_hidden($idcard) { $length = strlen($idcard); $calcLength = $length - 4 - 4; return substr_replace($idcard, str_repeat('*',$calcLength), 4, $calcLength); } /*===================================================== * 說明:銀行卡 隱藏 by:Simon *====================================================*/ function id_bank_card_hidden($card) { $length = strlen($card); $calcLength = $length - 4 - 4; return substr_replace($card, str_repeat('*',$calcLength), 4, $calcLength); } /*===================================================== * 說明:獲取當前日期 by:Simon *====================================================*/ function datetime() { return date('Y-m-d H:i:s', time()); } /*===================================================== * 函式:http_post_query * 說明:http請求 by:Simon ====================================================*/ function http_post_query($url, $data) { $postdata = http_build_query($data); $opts = array('http' => array( 'method' => 'POST', 'header' => 'Content-type: application/x-www-form-urlencoded', 'content' => $postdata ) ); $context = stream_context_create($opts); $result = file_get_contents($url, false, $context); return $result; } // =================================原始加入===================================== /** * 判斷手機號碼是否合法 * @param $mobile 手機號碼 * @return bool */ function check_mobile($mobile) { // $pattern = '/^(1(([35][0-9])|(47)|[8][0126789]))\d{8}$/'; $pattern = '/^(0|86|17951)?(888|13[0-9]|15[012356789]|17[0-9]|111|18[0-9]|14[57])[0-9]{8}$/'; if (preg_match($pattern, $mobile)) { return TRUE; } else { return FALSE; } } /** * 判斷郵箱地址是否合法 * @param $email * @return bool */ function checkEmail($email) { $pattern = "/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i"; if (preg_match($pattern, $email)) { return TRUE; } else { return FALSE; } } /** * 判斷使用者名稱是否合法 * @param $name 小寫字母開頭,只能包含小寫字母3-16位 * @return bool */ function checkUsername($name) { $patten = '/^[a-z]{3,12}$/'; if (preg_match($patten, $name)) { return TRUE; } else { return FALSE; } } /** * 判斷真實姓名是否合法 * @param $realname * @return bool */ function checkRealname($realname) { $patten = '/^[\x{4e00}-\x{9fa5}]+$/u'; if (preg_match($patten, $realname)) { return TRUE; } else { return FALSE; } } /** * 判斷IP地址是否合法 * @param $ip * @return bool */ function checkIP($ip) { $patten = "/^(((1?\d{1,2})|(2[0-4]\d)|(25[0-5]))\.){3}((1?\d{1,2})|(2[0-4]\d)|(25[0-5]))$/"; if (preg_match($patten, $ip)) { return true; } else { return false; } } /** * 格式化列印資料 * @param $data * @param bool $ext */ function pr($data, $ext = false) { //header("Content-type:text/html;charset=utf-8"); echo '<pre>'; print_r($data); echo '</pre>'; if ($ext) { exit; } } /** * 格式化列印資料 * @param $data * @param bool $ext * @auther dengqihua */ function __print_r($data, $ext = false) { //header("Content-type:text/html;charset=utf-8"); echo '<pre>'; print_r($data); echo '</pre>'; if ($ext) { exit; } } /** * 陣列層級縮排轉換 * @param array $array * @param int $pid * @param int $level * @return array */ function array2level($array, $pid = 0, $level = 1, &$result = []) { //static $list = []; foreach ($array as $v) { if ($v['parent_id'] == $pid) { $v['level'] = $level; $result[] = $v; array2level($array, $v['id'], $level + 1, $result); } } return $result; } /** * 構建層級(樹狀)陣列 * @param array $array 要進行處理的一維陣列,經過該函式處理後,該陣列自動轉為樹狀陣列 * @param string $pid 父級ID的欄位名 * @param string $child_key_name 子元素鍵名 * @return array|bool */ function array2tree(&$array, $pid = 'parent_id', $child_key_name = 'children') { $counter = array_children_count($array, $pid); if ($counter[0] == 0) return false; $tree = []; while (isset($counter[0]) && $counter[0] > 0) { $temp = array_shift($array); if (isset($counter[$temp['id']]) && $counter[$temp['id']] > 0) { array_push($array, $temp); } else { if ($temp[$pid] == 0) { $tree[] = $temp; } else { $array = array_child_append($array, $temp[$pid], $temp, $child_key_name); } } $counter = array_children_count($array, $pid); } return $tree; } /** * 子元素計數器 * @param $array * @param $pid * @return array */ function array_children_count($array, $pid) { $counter = []; foreach ($array as $item) { $count = isset($counter[$item[$pid]]) ? $counter[$item[$pid]] : 0; $count++; $counter[$item[$pid]] = $count; } return $counter; } /** * 把元素插入到對應的父元素$child_key_name欄位 * @param $parent * @param $pid * @param $child * @param string $child_key_name 子元素鍵名 * @return mixed */ function array_child_append($parent, $pid, $child, $child_key_name) { foreach ($parent as &$item) { if ($item['id'] == $pid) { if (!isset($item[$child_key_name])) $item[$child_key_name] = []; $item[$child_key_name][] = $child; } } return $parent; } /** * 獲取客戶端瀏覽器資訊 * @param $agent * @return string */ function getBrowser($agent) { $browser = ''; $browser_ver = ''; if (preg_match('/OmniWeb\/(v*)([^\s|;]+)/i', $agent, $regs)) { $browser = 'OmniWeb'; $browser_ver = $regs[2]; } if (preg_match('/Netscape([\d]*)\/([^\s]+)/i', $agent, $regs)) { $browser = 'Netscape'; $browser_ver = $regs[2]; } if (preg_match('/safari\/([^\s]+)/i', $agent, $regs)) { $browser = 'Safari'; $browser_ver = $regs[1]; } if (preg_match('/MSIE\s([^\s|;]+)/i', $agent, $regs)) { $browser = 'Internet Explorer'; $browser_ver = $regs[1]; } if (preg_match('/rv:([^\s|)]+)/i', $agent, $regs)) { $browser = 'Internet Explorer'; $browser_ver = $regs[1]; } if (preg_match('/Opera[\s|\/]([^\s]+)/i', $agent, $regs)) { $browser = 'Opera'; $browser_ver = $regs[1]; } if (preg_match('/NetCaptor\s([^\s|;]+)/i', $agent, $regs)) { $browser = '(Internet Explorer ' . $browser_ver . ') NetCaptor'; $browser_ver = $regs[1]; } if (preg_match('/Maxthon/i', $agent, $regs)) { $browser = '(Internet Explorer ' . $browser_ver . ') Maxthon'; $browser_ver = ''; } if (preg_match('/360SE/i', $agent, $regs)) { $browser = '(Internet Explorer ' . $browser_ver . ') 360SE'; $browser_ver = ''; } if (preg_match('/SE 2.x/i', $agent, $regs)) { $browser = '(Internet Explorer ' . $browser_ver . ') 搜狗'; $browser_ver = ''; } if (preg_match('/FireFox\/([^\s]+)/i', $agent, $regs)) { $browser = 'FireFox'; $browser_ver = $regs[1]; } if (preg_match('/Lynx\/([^\s]+)/i', $agent, $regs)) { $browser = 'Lynx'; $browser_ver = $regs[1]; } if (preg_match('/Chrome\/([^\s]+)/i', $agent, $regs)) { $browser = 'Chrome'; $browser_ver = $regs[1]; } if ($browser != '') { return $browser . ' ' . $browser_ver; } else { return '未知瀏覽器'; } } /** * 獲取客戶端作業系統資訊 * @param $agent * @return bool|string */ function getOs($agent) { $os = false; if(preg_match('/win/i', $agent) && preg_match('/nt 6.0/i', $agent)) { $os = 'Windows Vista'; } else if (preg_match('/win/i', $agent) && preg_match('/nt 6.1/i', $agent)) { $os = 'Windows 7'; } else if (preg_match('/win/i', $agent) && preg_match('/nt 6.2/i', $agent)) { $os = 'Windows 8'; } else if (preg_match('/win/i', $agent) && preg_match('/nt 10.0/i', $agent)) { $os = 'Windows 10';#新增win10判斷 } else if (preg_match('/win/i', $agent) && preg_match('/nt 5.1/i', $agent)) { $os = 'Windows XP'; } else if (preg_match('/win/i', $agent) && preg_match('/nt 5/i', $agent)) { $os = 'Windows 2000'; } else if (preg_match('/win/i', $agent) && preg_match('/nt/i', $agent)) { $os = 'Windows NT'; } else if (preg_match('/win/i', $agent) && preg_match('/32/i', $agent)) { $os = 'Windows 32'; } else if (preg_match('/linux/i', $agent)) { $os = 'Linux'; } else if (preg_match('/unix/i', $agent)) { $os = 'Unix'; } else if (preg_match('/sun/i', $agent) && preg_match('/os/i', $agent)) { $os = 'SunOS'; } else if (preg_match('/ibm/i', $agent) && preg_match('/os/i', $agent)) { $os = 'IBM OS/2'; } else if (preg_match('/Mac/i', $agent) && preg_match('/PC/i', $agent)) { $os = 'Macintosh'; } else if (preg_match('/PowerPC/i', $agent)) { $os = 'PowerPC'; } else if (preg_match('/AIX/i', $agent)) { $os = 'AIX'; } else if (preg_match('/HPUX/i', $agent)) { $os = 'HPUX'; } else if (preg_match('/NetBSD/i', $agent)) { $os = 'NetBSD'; } else if (preg_match('/BSD/i', $agent)) { $os = 'BSD'; } else if (preg_match('/OSF1/i', $agent)) { $os = 'OSF1'; } else if (preg_match('/IRIX/i', $agent)) { $os = 'IRIX'; } else if (preg_match('/FreeBSD/i', $agent)) { $os = 'FreeBSD'; } else if (preg_match('/teleport/i', $agent)) { $os = 'teleport'; } else if (preg_match('/flashget/i', $agent)) { $os = 'flashget'; } else if (preg_match('/webzip/i', $agent)) { $os = 'webzip'; } else if (preg_match('/offline/i', $agent)) { $os = 'offline'; } else { $os = '未知作業系統'; } return $os; } /** * 根據二維陣列的某個欄位進行排序 * @param $multi_array * @param $sort_key * @param int $sort * @return bool */ function array_multi_sort($multi_array, $sort_key, $sort = SORT_DESC) { if (is_array($multi_array)) { foreach ($multi_array as $row_array) { if (is_array($row_array)) { $key_array[] = $row_array[$sort_key]; } else { return false; } } } else { return false; } array_multisort($key_array, $sort, $multi_array); return $multi_array; } /** * httpPost請求 * @param url 請求地址 * @param data 請求引數(json) */ function post_json_response($url, $data) { $ch = curl_init(); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json; charset=utf-8', 'Content-Length: ' . strlen($data) ]); $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); return $response; } function millisecond() { list($t1, $t2) = explode(' ', microtime()); return (float)sprintf('%.0f', (floatval($t1) + floatval($t2))); } /* * api統一介面返回 * $code 狀態碼 * $res 返回資料結果集 (如果有) */ function json_api($code='00000000', $res=[], $codeMessage=[]){ $code = $code; $res = $res; if($codeMessage != []){ $returnMessageTpl = config('message.'.$code); // $return_msg = vprintf($returnMessageTpl, $codeMessage); $return_msg = sprintf($returnMessageTpl, ...$codeMessage); }else{ $return_msg = config('message.'.$code); } return json([ 'code' => $code, 'msg' => $return_msg, 'res' => $res ]); }