程序中总会有一些常用的php函数,总结一下:
<?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
]);
}