脱敏邮箱、电话
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | /** * 脱敏邮箱、电话 */ public function hidtel($phone) { //隐藏邮箱 if (strpos($phone, '@')) { $email_array = explode("@", $phone); $prevfix = (strlen($email_array[0]) < 4) ? "" : substr($phone,0,3); //邮箱前缀 $count = 0; $str = preg_replace('/([\d\w+_-]{0,100})@/','***@',$phone,-1,$count); $result = $prevfix.$str; } else { //隐藏联系方式中间4位 $IsWhat = preg_match('/(0[0-9]{2,3}[\-]?[2-9][0-9]{6,7}[\-]?[0-9]?)/i',$phone); //固定电话 if ($IsWhat == 1) { $result = preg_replace('/(0[0-9]{2,3}[\-]?[2-9])[0-9]{3,4}([0-9]{3}[\-]?[0-9]?)/i','$1****$2',$phone); } else { $result = preg_replace('/(1[0-9]{1}[0-9])[0-9]{4}([0-9]{4})/i','$1****$2',$phone); } } return $result; } |
代码加密
1 2 3 4 5 6 7 8 9 10 | /** * 代码加密 * @param string $pass 需要加密的字符串 * @return string 加密过后的字符串 */ public function pass_salt($pass) { $salt = 'selfpassword'; //盐值 return md5($salt.$pass); } |
唯一订单号生成
1 2 3 4 5 6 7 8 | /** * 唯一订单号生成 * @param $prefix 订单号前缀 */ public function orderNo($prefix = "SK") { return $prefix.(strtotime(date('YmdHis', time()))) . substr(microtime(), 2, 6) . sprintf('%03d', rand(0, 999)); } |
递归
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | /** * 递归 */ public function tree_data($data, $pid=0, $p, $s) { $arr = array(); foreach ($data as $v) { if ($v[$p]==$pid) { $v[$s] = $this->tree_data($data,$v['id'],$p,$s); $arr[]=$v; } } return $arr; } |
递归获取下级数组,可指定ID,一维数组
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | /** * 递归获取下级数组,可指定ID,一维数组 * @param array $data [description] * @param int $pid [description] * @param array $SonNode [description] * @return array 一维数组 */ public function getSonNode($data, $pid = 0, $SonNode = array()) { $SonNode[] = $pid; foreach($data as $k=>$v){ if($v['parentid'] == $pid){ $SonNode = $this->getSonNode($data,$v['id'],$SonNode); } } return $SonNode; } |
判断是否是https
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | /** * 判断是否是https */ public function isHTTPS() { if (defined('HTTPS') && HTTPS) return true; if (!isset($_SERVER)) return false; if (!isset($_SERVER['HTTPS'])) return false; if ($_SERVER['HTTPS'] === 1) { //Apache return true; } elseif ($_SERVER['HTTPS'] === 'on') { //IIS return true; } elseif ($_SERVER['SERVER_PORT'] == 443) { //其他 return true; } return false; } |
防止sql注入
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | /** * 防止sql注入 * $name = sql_check_param(I('name')); */ public function sql_check_param($value) { return $this->post_check($this->check_param($value)); } /** * 防止sql注入自定义方法一 * @param: mixed $value 参数值 */ public function check_param($value=null) { $str = 'select|insert|and|or|update|delete|\'|\/\*|\*|\.\.\/|\.\/|union|into|load_file|outfile'; if (eregi($str, $value)) { return ''; } return $value; } /** * 防止sql注入自定义方法二 * @param: mixed $value 参数值 */ public function post_check($value) { if(!get_magic_quotes_gpc()) { // 进行过滤 $value = addslashes($value); } $value = str_replace("_", "\_", $value); $value = str_replace("%", "\%", $value); $value = nl2br($value); $value = htmlspecialchars($value); return $value; } |
对象转数组
1 2 3 4 5 6 7 8 9 10 11 | /** * 对象转数组 * 空对象定义方法如下: * $obj=new \stdClass * $obj=new class{} * $obj=(object)[] */ public function objToArray($obj = new class{}) { return json_decode(json_encode($obj), true); } |
http_request CURL请求
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | /** * [http_request CURL请求] * @param [type] $url [请求url地址] * @param string $method [请求方法 GET POST] * @param [type] $params [数据数组] * @param array $headers [请求header信息] * @param [type] $debug [调试开启 默认false] * @param [type] $timeout [超时时间] * @return [type] [description] */ public function http_request($url, $method="GET", $params = null, $headers = array(), $debug = false, $timeout = 60) { $method = strtoupper($method); $ci = curl_init(); /* Curl settings */ curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); curl_setopt($ci, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0"); curl_setopt($ci, CURLOPT_CONNECTTIMEOUT,$timeout); /* 在发起连接前等待的时间,如果设置为0,则无限等待 */ curl_setopt($ci, CURLOPT_TIMEOUT, 7); /* 设置cURL允许执行的最长秒数 */ curl_setopt($ci, CURLOPT_RETURNTRANSFER, true); switch ($method) { case "POST": curl_setopt($ci, CURLOPT_POST, true); if (!empty($params)) { $tmpdatastr = is_array($params) ? http_build_query($params) : $params; curl_setopt($ci, CURLOPT_POSTFIELDS, $tmpdatastr); } break; default: $query_string = is_array($params) ? http_build_query($params) : $params; $url = $query_string ? $url . (stripos($url, "?") !== false ? "&" : "?") . $query_string : $url; curl_setopt($ci, CURLOPT_CUSTOMREQUEST, $method); /* //设置请求方式 */ break; } $ssl = preg_match('/^https:\/\//i', $url) ? TRUE : FALSE; curl_setopt($ci, CURLOPT_URL, $url); if ($ssl) { curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, FALSE); // 不从证书中检查SSL加密算法是否存在 } //curl_setopt($ci, CURLOPT_HEADER, true); /*启用时会将头文件的信息作为数据流输出*/ if (ini_get('open_basedir') == '' && ini_get('safe_mode' == 'Off')) { curl_setopt($ci, CURLOPT_FOLLOWLOCATION, 1); } curl_setopt($ci, CURLOPT_MAXREDIRS, 2);/*指定最多的HTTP重定向的数量,这个选项是和CURLOPT_FOLLOWLOCATION一起使用的*/ curl_setopt($ci, CURLOPT_HTTPHEADER, $headers); curl_setopt($ci, CURLINFO_HEADER_OUT, true); /*curl_setopt($ci, CURLOPT_COOKIE, $Cookiestr); * *COOKIE带过去** */ $response = curl_exec($ci); $requestinfo = curl_getinfo($ci); $http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE); if ($debug) { echo "=====post data======\r\n"; var_dump($params); echo "========info======== \r\n"; print_r($requestinfo); echo "======response======\r\n"; print_r($response); } curl_close($ci); return $response; //return array($http_code, $response,$requestinfo); } |
数组参数ascii码排序生成字符串参数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | /** * 数组参数ascii码排序生成字符串参数 * [多用于支付接口的ascii排序传参] */ public function strToAscii($param = NULL, $urlencode = FALSE) { $str = ''; ksort($param); foreach($param as $k=>$v){ if($v != null && $v != 'null' && $k != 'sign'){ if($urlencode){ $v = urlencode($v); //url加密,不需要不加 } $str .= $k . '=' . $v . '&'; } } if(strlen($str) > 0){ $newStr = substr($str, 0,strlen($str) - 1); } return $newStr; } |
二维数组排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | /** * 二维数组排序 * @param $arr * @param $keys * @param string $type * @return array */ public function array_sort($arr, $keys, $type = 'desc') { $key_value = $new_array = array(); foreach ($arr as $k => $v) { $key_value[$k] = $v[$keys]; } if ($type == 'asc') { asort($key_value); } else { arsort($key_value); } reset($key_value); foreach ($key_value as $k => $v) { $new_array[$k] = $arr[$k]; } return $new_array; } |
格式化字节大小
1 2 3 4 5 6 7 8 9 10 11 | /** * 格式化字节大小 * @param number $size 字节数 * @param string $delimiter 数字和单位分隔符 * @return string 格式化后的带单位的大小 */ public function format_bytes($size, $delimiter = '') { $units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB'); for ($i = 0; $size >= 1024 && $i < 5; $i++) $size /= 1024; return round($size, 2) . $delimiter . $units[$i]; } |
获取ip地址物理位置(貌似已经弃用)
1 2 3 4 5 6 7 8 9 10 11 12 13 | /** * 获取ip地址物理位置,貌似已经弃用 * 地址有变化,https://ip.taobao.com/ipSearch?ipAddr=120.99.6.200 */ public function findCityByIp($ip){ $data = file_get_contents('https://ip.taobao.com/service/getIpInfo.php?ip='.$ip); $dizhi = json_decode($data,$assoc=true); $country = $dizhi['data']['country']; if (($country == "台湾") || ($country == "澳门") || ($country == "香港")) { $country = "中国".$country; } return $country." ".$dizhi['data']['region']." ".$dizhi['data']['city']." ".$dizhi['data']['isp']; } |
获取客户端浏览器类型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | /** * 获取客户端浏览器类型 * @return string 浏览器类型返回字符串 unknown为未知浏览器类型 */ public function get_browser_type(){ $agent = $_SERVER["HTTP_USER_AGENT"]; if(strpos($agent,'MSIE')!==false || strpos($agent,'rv:11.0')){ //ie11判断 return "ie"; }else if(strpos($agent,'Firefox')!==false){ return "firefox"; }else if(strpos($agent,'Chrome')!==false){ return "chrome"; }else if(strpos($agent,'Opera')!==false){ return 'opera'; }else if((strpos($agent,'Chrome')==false)&&strpos($agent,'Safari')!==false){ return 'safari'; }else{ return 'unknown'; } } |
判断是否是移动端
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | /** * 判断是否是移动端 */ public function is_mobile() { // 如果有HTTP_X_WAP_PROFILE则一定是移动设备 if (isset ($_SERVER['HTTP_X_WAP_PROFILE'])) return true; //此条摘自TPM智能切换模板引擎,适合TPM开发 if(isset ($_SERVER['HTTP_CLIENT']) &&'PhoneClient'==$_SERVER['HTTP_CLIENT']) return true; //如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息 if (isset ($_SERVER['HTTP_VIA'])) //找不到为flase,否则为true return stristr($_SERVER['HTTP_VIA'], 'wap') ? true : false; //判断手机发送的客户端标志,兼容性有待提高 if (isset ($_SERVER['HTTP_USER_AGENT'])) { $clientkeywords = array( 'nokia','sony','ericsson','mot','samsung','htc','sgh','lg','sharp', 'sie-','philips','panasonic','alcatel','lenovo','iphone','ipod', 'blackberry','meizu','android','netfront','symbian','ucweb','windowsce', 'palm','operamini','operamobi','openwave','nexusone','cldc','midp','wap','mobile' ); //从HTTP_USER_AGENT中查找手机浏览器的关键字 if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))) { return true; } } //协议法,因为有可能不准确,放到最后判断 if (isset ($_SERVER['HTTP_ACCEPT'])) { // 如果只支持wml并且不支持html那一定是移动设备 // 如果支持wml和html但是wml在html之前则是移动设备 if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html')))) { return true; } } return false; } |
随机生成要求位数个字符串
1 2 3 4 5 6 7 8 9 10 11 12 13 | /** * 随机生成要求位数个字符串 * @param length 规定几位字符 */ public function getRandChar($length){ $str = null; $strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";//大小写字母以及数字 $max = strlen($strPol) - 1; for($i=0;$i<$length;$i++){ $str .= $strPol[rand(0,$max)]; } return $str; } |
获取图片主要颜色
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | /** * 获取图片主要颜色 * @param $image * @return array */ public function dominant_color($image) { $rTotal = $gTotal = $bTotal = $total = 0; $type = getimagesize($image);//取得图片的大小,类型等 if (isset($type['mime']) && $type['mime'] == 'image/webp') { $i = imagecreatefromwebp($image); }else{ $i = imagecreatefromstring(file_get_contents($image)); } //$i = imagecreatefromjpeg($image); for ($x = 0; $x < imagesx($i); $x++) { for ($y = 0; $y < imagesy($i); $y++) { $rgb = imagecolorat($i, $x, $y); $r = ($rgb >> 16) & 0xFF; $g = ($rgb >> 8) & 0xFF; $b = $rgb & 0xFF; $rTotal += $r; $gTotal += $g; $bTotal += $b; $total++; } } $rAverage = round($rTotal / $total); $gAverage = round($gTotal / $total); $bAverage = round($bTotal / $total); return array( 'r' => $rAverage, 'g' => $gAverage, 'b' => $bAverage, ); } |
设置图片文字水印(针对thinkphp3来举例)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | /** * 设置图片文字水印(针对thinkphp3来举例) * 根据图片的整体颜色设置差不多的水印字体颜色 * @param $img_url 打开图片路径 * @param $save_url 保存图片路径 * @param $waterText 默认水印文字 * @return $save_url 图片最终保存位置即生成水印图片地址 * 参考 https://www.php.cn/php-ask-484420.html * 参考 https://www.codenong.com/32962624/ */ public function setImageTextWater($img_url = null, $save_url = null, $waterText = '水印文字') { ($img_url == null || $save_url == null) && return false; $image = new \Think\Image(); $image->open($img_url); //打开文件位置 $width = $image->width(); $height = $image->height(); $image->crop($width, $height)->save($save_url); //裁剪保存到新的位置 $color = $this->dominant_color($save_url); // 将rgb转换为十六进制颜色代码 $final_color = sprintf("#%02x%02x%02x", $color['r'], $color['g'], $color['b']); $text_url = './Public/simhei.ttf'; //水印字体位置 $image->text($waterText, $text_url, 25, $final_color, 9, 15, 30); $image->save($save_url); //加完水印再保存 return ($save_url==null||$save_url=="")? false: $save_url; } |
随机生成手机号,并且返回不重复手机号
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | /** * 随机生成手机号,并且返回不重复手机号 */ public function randNum($phoneStr = array()) { $phoneList = array('137', '138', '139', '189', '188', '135', '176', '183', '150'); $finalArr = array_filter(array_unique(array_merge($phoneList, $phoneStr)));//合并,去重,去空值 $key = array_rand($finalArr, 1); $rand = rand(11111111, 99999999); $phone = $finalArr[$key].$rand; $data = M('yonghu')->where(array('gongsiid'=>session('gongsiid'), 'dianhua'=>$phone))->find(); $data && $this->randNum($phoneStr); return $phone; } |
过滤字符串,只保留中文、英文以及数字,去掉其他全部符号
1 2 3 4 5 6 7 8 9 10 11 12 | /** * 过滤字符串,只保留中文、英文以及数字,去掉其他全部符号 */ public function filter_string($str = "") { //preg_match_all()函数可以配合正则表达式来过滤字符串,只保留中文、英文以及数字,去掉其他全部符号 $str = "上海xxx网络有限公司第一百一十八\r\n \t\t\t\t\t\t\t\t\t\t\t分店"; //举例目标字符串 preg_match_all("/[\x{4e00}-\x{9fa5}a-zA-Z0-9]/u", $str, $result); $newStr = join('', $result[0]); //上海xxx网络有限公司第一百一十八分店 return $newStr; } |