PHP的常用方法
判断是否含有中文
/** * 判断字符串是否含有中文 * @param string $str * 返回bool */ function is_Chinese($str){ if (preg_match("/[\x7f-\xff]/", $str)) { return true; }else{ return false; } }
多维数组转一维数组
/** * 多维数组转一维数组 * @param array $data * @return array */ function convertArray($data):array{ $arr = []; foreach($data as $k=>$v){ if(is_array($v)){ $arr = array_merge($arr,convertArray($v)); }else{ $arr[] = $v; } } return $arr; }
获取本周每天的时间戳 , (周一到周日的时间戳)
/** * 获取本周每天的时间戳(周一到周日) * @return array */ function get_week_arr(){ //获取今天是周几,0为周日 $this_week_num = date('w'); $timestamp = time(); //如果获取到的日期是周日,需要把时间戳换成上一周的时间戳 //英语国家 一周的开始时间是周日 if($this_week_num == 0){ $timestamp = $timestamp - 86400; } $this_week_arr = [ [ 'is_sign'=>0, 'this_week'=>1, 'week_name'=>'星期一', 'week_time'=>strtotime(date('Y-m-d', strtotime("this week Monday", $timestamp))), ], [ 'is_sign'=>0, 'this_week'=>2, 'week_name'=>'星期二', 'week_time'=>strtotime(date('Y-m-d', strtotime("this week Tuesday", $timestamp))), ], [ 'is_sign'=>0, 'this_week'=>3, 'week_name'=>'星期三', 'week_time'=>strtotime(date('Y-m-d', strtotime("this week Wednesday", $timestamp))), ], [ 'is_sign'=>0, 'this_week'=>4, 'week_name'=>'星期四', 'week_time'=>strtotime(date('Y-m-d', strtotime("this week Thursday", $timestamp))), ], [ 'is_sign'=>0, 'this_week'=>5, 'week_name'=>'星期五', 'week_time'=>strtotime(date('Y-m-d', strtotime("this week Friday", $timestamp))), ], [ 'is_sign'=>0, 'this_week'=>6, 'week_name'=>'星期六', 'week_time'=>strtotime(date('Y-m-d', strtotime("this week Saturday", $timestamp))), ], [ 'is_sign'=>0, 'this_week'=>7, 'week_name'=>'星期天', 'week_time'=>strtotime(date('Y-m-d', strtotime("this week Sunday", $timestamp))), ], ]; return $this_week_arr; }
获取富文本数据中的纯字符串
/** * 获取纯文本 * @param string $result * @param int $lenth * @return void */ function get_text($result,$lenth=150){ $result = strip_tags($result); $result= str_replace(array("\r","\n","\s","\t"," "," "),'',$result); $result = mb_substr($result,0,$lenth,'utf-8'); return $result; }
过滤数组中已匹配字符串
/** * 过滤数组中已匹配字符 * * @param array $data * @param string $str * @return array */ function glarray($data=[],$str=''){ foreach($data as $k=>$v){ if(strstr($v, $str)){ unset($data[$k]); } } return $data; }
获取访客的操作系统
/** * 获得访客操作系统 */ function get_os() { if (!empty($_SERVER['HTTP_USER_AGENT'])) { $os = $_SERVER['HTTP_USER_AGENT']; if (preg_match('/win/i', $os)) { $os = 'Windows'; } else if (preg_match('/mac/i', $os)) { $os = 'MAC'; } else if (preg_match('/linux/i', $os)) { $os = 'Linux'; } else if (preg_match('/unix/i', $os)) { $os = 'Unix'; } else if (preg_match('/bsd/i', $os)) { $os = 'BSD'; } else { $os = 'Other'; } return $os; } else { return 'unknow'; } }
获取访客的浏览器
/** * 获得访问者浏览器 */ //方法一 function browse_info() { if (!empty($_SERVER['HTTP_USER_AGENT'])) { $br = $_SERVER['HTTP_USER_AGENT']; if (preg_match('/MSIE/i', $br)) { $br = 'MSIE'; } else if (preg_match('/Firefox/i', $br)) { $br = 'Firefox'; } else if (preg_match('/Chrome/i', $br)) { $br = 'Chrome'; } else if (preg_match('/Safari/i', $br)) { $br = 'Safari'; } else if (preg_match('/Opera/i', $br)) { $br = 'Opera'; } else { $br = '其他浏览器'; } return $br; } else { return '未知浏览器'; } } //方法二 function my_get_browser(){ if(empty($_SERVER['HTTP_USER_AGENT'])){ return 'robot!'; } if( (false == strpos($_SERVER['HTTP_USER_AGENT'],'MSIE')) && (strpos($_SERVER['HTTP_USER_AGENT'], 'Trident')!==FALSE) ){ return 'IE 11.0'; } if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'MSIE 10.0')){ return 'IE 10.0'; } if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'MSIE 9.0')){ return 'IE 9.0'; } if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'MSIE 8.0')){ return 'IE 8.0'; } if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'MSIE 7.0')){ return 'IE 7.0'; } if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'MSIE 6.0')){ return 'IE 6.0'; } if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'Edge')){ return 'Edge'; } if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'Firefox')){ return 'Firefox'; } if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'Chrome')){ return 'Chrome'; } if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'Safari')){ return 'Safari'; } if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'Opera')){ return 'Opera'; } if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'360SE')){ return '360SE'; } //微信浏览器 if(false!==strpos($_SERVER['HTTP_USER_AGENT'],'MicroMessage')){ return 'MicroMessage'; } return '未知浏览器'; }
获取访问者浏览器语言
/** * 获得访问者浏览器语言 */ function get_lang() { if (!empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { $lang = $_SERVER['HTTP_ACCEPT_LANGUAGE']; $lang = substr($lang, 0, 5); if (preg_match('/zh-cn/i',$lang)) { $lang = '简体中文'; } else if (preg_match('/zh/i',$lang)) { $lang = '繁体中文'; } else { $lang = 'English'; } return $lang; } else { return 'unknow'; } }
上一篇 : php查询ip归属地
下一篇 : PHP过滤一些基本的XSS和SQL注入
-------------本文结束感谢您的阅读-------------