/** * 判断AJAX */ function isAjax() { if ((isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') && !isset($_GET['ajax'])) { return true; } else { return false; } } /** * 判断GET */ function isGet() { if ($_SERVER['REQUEST_METHOD'] == 'GET') { return true; } else { return false; } } /** * 判断POST */ function isPost() { if ($_SERVER['REQUEST_METHOD'] == 'POST') { return true; } else { return false; } } /** * 判断微信访问 * @return bool */ function isWechat() { if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false) { return true; } return false; } /** * 判断APP访问 * @return bool */ function isApp() { if ($_SERVER['HTTP_FROM'] == 'app') { return true; } else { return false; } } /** * 数据签名 * @param $data * @return mixed */ function data_sign($data) { $config = Config::get('dux.use'); if (!is_array($data)) { $data = [ 'data' => $data ]; } ksort($data); return base64_encode(hash_hmac('sha1', http_build_query($data), $config['safe_key'], true)); } /** * 验证签名 * @param $data * @param string $sign * @return bool */ function data_sign_has($data, $sign = '') { if(empty($sign)) { return false; } if (!is_array($data)) { $data = [ 'data' => $data ]; } $sign = base64_decode($sign); ksort($data); $config = Config::get('dux.use'); $valToken = hash_hmac('sha1', http_build_query($data), $config['safe_key'], true); return ($sign == $valToken); } /** * 时间格式化 * @param $time * @return string */ function date_tran($time) { $agoTime = (int)$time; // 计算出当前日期时间到之前的日期时间的毫秒数,以便进行下一步的计算 $time = time() - $agoTime; if ($time >= 31104000) { // N年前 $num = (int)($time / 31104000); return $num . '年前'; } if ($time >= 2592000) { // N月前 $num = (int)($time / 2592000); return $num . '月前'; } if ($time >= 86400) { // N天前 $num = (int)($time / 86400); return $num . '天前'; } if ($time >= 3600) { // N小时前 $num = (int)($time / 3600); return $num . '小时前'; } if ($time > 60) { // N分钟前 $num = (int)($time / 60); return $num . '分钟前'; } return '刚刚'; } /** * 判断UTF-8 * @param string $string 字符串 * @return boolean */ public static function isUtf8($string) { if (!empty($string)) { $ret = json_encode(['code' => $string]); if ($ret == '{"code":null}') { return false; } } return true; } /** * 转义html * @param $str * @return string */ public static function htmlIn($str) { if (function_exists('htmlspecialchars')) { $str = htmlspecialchars($str); } else { $str = htmlentities($str); } $str = addslashes($str); return $str; } /** * html代码还原 * @param $str * @return string */ public static function htmlOut($str) { if (function_exists('htmlspecialchars_decode')) { $str = htmlspecialchars_decode($str); } else { $str = html_entity_decode($str); } $str = stripslashes($str); return $str; } /** * html代码清理 * @param $str * @return string */ public static function htmlClear($str) { $str = self::htmlOut($str); $xss = new duxvendorHtmlCleaner(); return $xss->remove($str); } /** * 符号过滤 * @param $text * @return string */ public static function symbolClear($text) { if (trim($text) == '') return ''; $text = preg_replace("/[[:punct:]s]/", ' ', $text); $text = urlencode($text); $text = preg_replace("/(%7E|%60|%21|%40|%23|%24|%25|%5E|%26|%27|%2A|%28|%29|%2B|%7C|%5C|%3D|-|_|%5B|%5D|%7D|%7B|%3B|%22|%3A|%3F|%3E|%3C|%2C|.|%2F|%A3%BF|%A1%B7|%A1%B6|%A1%A2|%A1%A3|%A3%AC|%7D|%A1%B0|%A3%BA|%A3%BB|%A1%AE|%A1%AF|%A1%B1|%A3%FC|%A3%BD|%A1%AA|%A3%A9|%A3%A8|%A1%AD|%A3%A4|%A1%A4|%A3%A1|%E3%80%82|%EF%BC%81|%EF%BC%8C|%EF%BC%9B|%EF%BC%9F|%EF%BC%9A|%E3%80%81|%E2%80%A6%E2%80%A6|%E2%80%9D|%E2%80%9C|%E2%80%98|%E2%80%99|%EF%BD%9E|%EF%BC%8E|%EF%BC%88)+/", ' ', $text); $text = urldecode($text); return trim($text); } /** * 随机字符串 * @param int $length * @return string */ public static function randStr($length = 5) { $pattern = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLOMNOPQRSTUVWXYZ'; $key = ''; for ($i = 0; $i < $length; $i++) { $key .= $pattern{mt_rand(0, 35)}; } return $key; } /** * 格式化为数字(忽略限制) * @param $str * @return int|mixed */ public static function intFormat($str) { $str = preg_replace('/[^-d]*(-?d*).*/', '$1', $str); return $str ? $str : 0; } /** * 价格格式化不带千分位 * @param $money * @return string */ public static function priceFormat($str) { if (empty($str)) { return $str = 0; } return @number_format($str, 2, ".", ""); } /** * CURL POST数据 * @param string $url 发送地址 * @param array $post_data 发送数组 * @param integer $timeout 超时秒 * @param string $header 头信息 * @return string */ static public function curlPost($url, $post_data=array(), $timeout=5,$header="") { $header=empty($header)?'':$header; $post_string = http_build_query($post_data); $ch = curl_init(); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); curl_setopt($ch, CURLOPT_REFERER, $_SERVER['HTTP_HOST']); curl_setopt($ch, CURLOPT_HTTPHEADER, array($header));//模拟的header头 $result = curl_exec($ch); curl_close($ch); return $result; } /** * CURL POST数据 * @param string $url 发送地址 * @param array $post_data 发送数组 * @param integer $timeout 超时秒 * @param string $header 头信息 * @return string */ static public function curlPost($url, $post_data=array(), $timeout=5,$header="") { $header=empty($header)?'':$header; $post_string = http_build_query($post_data); $ch = curl_init(); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); curl_setopt($ch, CURLOPT_REFERER, $_SERVER['HTTP_HOST']); curl_setopt($ch, CURLOPT_HTTPHEADER, array($header));//模拟的header头 $result = curl_exec($ch); curl_close($ch); return $result; } /** * 获取客户端系统语言 * @access public * @static true * @return string */ public static function getUserLang() { return htmlspecialchars($_SERVER['HTTP_ACCEPT_LANGUAGE']); } /** * 获取客户端IP * * @access public * @return string */ public static function getUserIp() { return htmlspecialchars($_SERVER['REMOTE_ADDR']); } /** * 获取当前页面的url来源 * * @access public * @return string */ public static function getUrlSource() { return htmlspecialchars($_SERVER['HTTP_REFERER']); } /** * 获取客户端浏览器信息. * * @access public * @return string */ public static function getUserAgent() { return htmlspecialchars($_SERVER['HTTP_USER_AGENT']); } /** * 获取客户端浏览器信息 * * @access public * @return string */ public static function getUserBrowser() { $userAgentInfo = htmlspecialchars($_SERVER['HTTP_USER_AGENT']); if (strpos($userAgentInfo, 'MSIE 9.0')) { return 'IE9'; } else if (strpos($userAgentInfo, 'MSIE 8.0')) { return 'IE8'; } else if (strpos($userAgentInfo, 'MSIE 7.0')) { return 'IE7'; } else if (strpos($userAgentInfo, 'MSIE 6.0')) { return 'IE6'; } else if (strpos($userAgentInfo, 'Firefox')) { return 'Firfox'; } else if (strpos($userAgentInfo, 'Chrome')) { return 'Chrome'; } else if (strpos($userAgentInfo, 'Opera')) { return 'Opera'; } else if (strpos($userAgentInfo, 'Safari')) { return 'Safari'; } else if (strpos($userAgentInfo, 'Elinks')) { return 'Elinks'; } else if (strpos($userAgentInfo, 'OmniWeb')) { return 'OmniWeb'; } else if (strpos($userAgentInfo, 'Links')) { return 'Links'; } else if (strpos($userAgentInfo, 'Lynx')) { return 'Lynx'; } else if (strpos($userAgentInfo, 'Arora')) { return 'Arora'; } else if (strpos($userAgentInfo, 'Epiphany')) { return 'Epiphany'; } else if (strpos($userAgentInfo, 'Konqueror')) { return 'Konqueror'; } else if (strpos($userAgentInfo, 'EudoraWeb')) { return 'EudoraWeb'; } else if (strpos($userAgentInfo, 'Minimo')) { return 'Minimo'; } else if (strpos($userAgentInfo, 'NetFront')) { return 'NetFront'; } else if (strpos($userAgentInfo, 'POLARIS')) { return 'Polaris'; } else if (strpos($userAgentInfo, 'BlackBerry')) { return 'BlackBerry'; } else if (strpos($userAgentInfo, 'Nokia')) { return 'Nokia'; } else { return 'Others'; } } /** * 获取客户端操作系统信息 * * @access public * @return string */ public static function getUserOs() { $userAgentInfo = htmlspecialchars($_SERVER['HTTP_USER_AGENT']); if (strpos($userAgentInfo, 'Windows NT 6.1')) { return 'Windows 7'; } else if (strpos($userAgentInfo, 'Windows NT 6.0')) { return 'Windows Vista'; } else if (strpos($userAgentInfo, 'Windows NT 5.2')) { return 'Windows 2003'; } else if (strpos($userAgentInfo, 'Windows NT 5.1')) { return 'Windows XP'; } else if (strpos($userAgentInfo, 'Windows NT 5.0')) { return 'Windows 2000'; } else if (strpos($userAgentInfo, 'Windows ME')) { return 'Windows ME'; } else if (strpos($userAgentInfo, 'PPC Mac OS X')) { return 'OS X PPC'; } else if (strpos($userAgentInfo, 'Intel Mac OS X')) { return 'OS X Intel'; } else if (strpos($userAgentInfo, 'Win98')) { return 'Windows 98'; } else if (strpos($userAgentInfo, 'Win95')) { return 'Windows 95'; } else if (strpos($userAgentInfo, 'WinNT4.0')) { return 'Windows NT4.0'; } else if (strpos($userAgentInfo, 'Mac OS X Mach-O')) { return 'OS X Mach'; } else if (strpos($userAgentInfo, 'Ubuntu')) { return 'Ubuntu'; } else if (strpos($userAgentInfo, 'Debian')) { return 'Debian'; } else if (strpos($userAgentInfo, 'AppleWebKit')) { return 'WebKit'; } else if (strpos($userAgentInfo, 'Mint/8')) { return 'Mint 8'; } else if (strpos($userAgentInfo, 'Minefield')) { return 'Minefield Alpha'; } else if (strpos($userAgentInfo, 'gentoo')) { return 'Gentoo'; } else if (strpos($userAgentInfo, 'Kubuntu')) { return 'Kubuntu'; } else if (strpos($userAgentInfo, 'Slackware/13.0')) { return 'Slackware 13'; } else if (strpos($userAgentInfo, 'Fedora')) { return 'Fedora'; } else if (strpos($userAgentInfo, 'FreeBSD')) { return 'FreeBSD'; } else if (strpos($userAgentInfo, 'SunOS')) { return 'SunOS'; } else if (strpos($userAgentInfo, 'OpenBSD')) { return 'OpenBSD'; } else if (strpos($userAgentInfo, 'NetBSD')) { return 'NetBSD'; } else if (strpos($userAgentInfo, 'DragonFly')) { return 'DragonFly'; } else if (strpos($userAgentInfo, 'IRIX')) { return 'IRIX'; } else if (strpos($userAgentInfo, 'Windows CE')) { return 'Windows CE'; } else if (strpos($userAgentInfo, 'PalmOS')) { return 'PalmOS'; } else if (strpos($userAgentInfo, 'Linux')) { return 'Linux'; } else if (strpos($userAgentInfo, 'DragonFly')) { return 'DragonFly'; } else if (strpos($userAgentInfo, 'Android')) { return 'Android'; } else if (strpos($userAgentInfo, 'Mac OS X')) { return 'Mac OS X'; } else if (strpos($userAgentInfo, 'iPhone')) { return 'iPhone OS'; } else if (strpos($userAgentInfo, 'Symbian OS')) { return 'Symbian'; } else if (strpos($userAgentInfo, 'Symbian OS')) { return 'Symbian'; } else if (strpos($userAgentInfo, 'SymbianOS')) { return 'SymbianOS'; } else if (strpos($userAgentInfo, 'webOS')) { return 'webOS'; } else if (strpos($userAgentInfo, 'PalmSource')) { return 'PalmSource'; } else { return 'Others'; } }
/** * 字符串截取 * @param $str * @param $length * @param bool $suffix * @param string $charset * @return string */ public static function strLen($str, $length, $suffix = true, $charset = "utf-8") { if ($charset != 'utf-8') { $str = mb_convert_encoding($str, 'utf8', $charset); } $osLen = mb_strlen($str); if ($osLen <= $length) { return $str; } $string = mb_substr($str, 0, $length, 'utf8'); $sLen = mb_strlen($string, 'utf8'); $bLen = strlen($string); $sCharCount = (3 * $sLen - $bLen) / 2; if ($osLen <= $sCharCount + $length) { $arr = preg_split('/(?<!^)(?!$)/u', mb_substr($str, $length + 1, $osLen, 'utf8')); //将中英混合字符串分割成数组(UTF8下有效) } else { $arr = preg_split('/(?<!^)(?!$)/u', mb_substr($str, $length + 1, $sCharCount, 'utf8')); } foreach ($arr as $value) { if (ord($value) < 128 && ord($value) > 0) { $sCharCount = $sCharCount - 1; } else { $sCharCount = $sCharCount - 2; } if ($sCharCount <= 0) { break; } $string .= $value; } if ($suffix) return $string . '…'; return $string; }