您好,欢迎访问一九零五行业门户网

PHP获取客户端操作系统,游览器类型及版本号

'edge', 'ie' => 'msie|iemobile|msiemobile|trident/[.0-9]+', 'chrome' => '(?:\bcrmo\b|crios|android)?.*chrome/[.0-9]* (mobile)?', 'opera' => 'opera.*mini|opera.*mobi|android.*opera|mobile.*opr/[0-9.]+|coast/[0-9.]+', 'firefox' => 'fennec|firefox.*maemo|(mobile|tablet).*firefox|firefox.*mobile', 'safari' => 'version.*mobile.*safari|safari.*mobile|mobilesafari', 'ucbrowser' => 'uc.*browser|ucweb', //uc游览器 'qqbrowser' => 'mqqbrowser|tencenttraveler', //qq游览器 'the world' => 'the world', //世界之窗游览器 'maxthon' => 'maxthon', //遨游游览器 'baiduboxapp' => 'baiduboxapp', 'baidubrowser' => 'baidubrowser', 'nokiabrowser' => 'nokia', ); /** * 操作系统正则 * note:移动设备的系统需优先匹配 * 故正则需要放在电脑系统前面 * @var array * @author wuzhc 2016-01-23 */ protected static $platforms = array( 'ios' => '\biphone.*mobile|\bipod|\bipad', 'windows mobile os' => 'windows ce.*(ppc|smartphone|mobile|[0-9]{3}x[0-9]{3})|window mobile|windows phone [0-9.]+|wce;', 'windows phone os' => 'windows phone 10.0|windows phone 8.1|windows phone 8.0|windows phone os|xblwp7|zunewp7|windows nt 6.[23]; arm;', 'android' => 'android', 'blackberry os' => 'blackberry|\bbb10\b|rim tablet os', //黑莓 'symbianos' => 'symbian|symbos|series60|series40|syb-[0-9]+|\bs60\b', //塞班 'webos' => 'webos|hpwos', 'micromessenger' => 'micromessenger', //微信 'windows' => 'windows', 'windows nt' => 'windows nt', 'mac os x' => 'mac os x', 'ubuntu' => 'ubuntu', 'linux' => 'linux', 'chrome os' => 'cros', ); /** * 版本号匹配正则(游览器 + 操作系统) * @var array * @author wuzhc 2016-01-22 */ protected static $versionregexs = array( // browser 'maxthon' => 'maxthon [ver]', //遨游 'chrome' => array('chrome/[ver]', 'crios/[ver]', 'crmo/[ver]'), //谷歌 'firefox' => 'firefox/[ver]', //火狐 'fennec' => 'fennec/[ver]', //火狐 'ie' => array('iemobile/[ver];', 'iemobile [ver]', 'msie [ver];', 'rv:[ver]'), 'opera' => array('opr/[ver]', 'opera mini/[ver]', 'version/[ver]', 'opera [ver]'), 'uc browser' => 'uc browser[ver]', //uc 'qqbrowser' => array('mqqbrowser/[ver]','tencenttraveler/[ver]'), //qq 'micromessenger'=> 'micromessenger/[ver]', //微信 'baiduboxapp' => 'baiduboxapp/[ver]', //百度盒子 'baidubrowser' => 'baidubrowser/[ver]', //百度 'safari' => array('version/[ver]', 'safari/[ver]' ), //mac os x中的浏览器 'nokiabrowser' => 'nokiabrowser/[ver]', //诺基亚 // os 'ios' => '\bi?os\b [ver][ ;]{1}', 'blackberry' => array('blackberry[\w]+/[ver]', 'blackberry.*version/[ver]', 'version/[ver]'), //黑莓手机 'windows phone os' => array( 'windows phone os [ver]', 'windows phone [ver]'), 'windows phone' => 'windows phone [ver]', 'windows nt' => 'windows nt [ver]', 'windows' => 'windows nt [ver]', 'symbianos' => array('symbianos/[ver]', 'symbian/[ver]'), //塞班系统 'webos' => array('webos/[ver]', 'hpwos/[ver];'), //lg 'mac os x' => 'mac os x [ver]', //苹果系统 'blackberry os' => array('blackberry[\w]+/[ver]', 'blackberry.*version/[ver]', 'version/[ver]'), 'android' => 'android [ver]', 'chrome os' => 'cros x86_64 [ver]', ); /** * 获取客户端游览器 * @param $useragent $_server['http_user_agent'] * @param bool $isreturnversion //是否一起返回版本号 * @return string (类型 + 版本号) * @author wuzhc 2016-01-23 */ public static function getbrowser($useragent,$isreturnversion = false) { if(empty($useragent)) { return ''; } $clientbrowser = ''; foreach((array)self::$browsers as $key => $browser) { if(self::match($browser,$useragent)) { $clientbrowser = $key; break; } } if($isreturnversion && $clientbrowser) { $clientbrowser .= ' '.self::getversion($clientbrowser,$useragent); } return $clientbrowser; } /** * 获取客户端操作系统 * @param $useragent * @param bool $isreturnversion //是否一起返回版本号 * @return string * @author wuzhc 2016-01-23 */ public static function getplatform($useragent,$isreturnversion = false) { if(empty($useragent)) { return ''; } $clientplatform = ''; foreach((array)self::$platforms as $key => $platform) { if(self::match($platform,$useragent)) { $clientplatform = $key; break; } } if($isreturnversion && $clientplatform) { $clientplatform .= ' '.self::getversion($clientplatform,$useragent); } return $clientplatform; } /** * 查询版本号 * @param $propertyname (操作系统名称和或游览器名称) * @see self::$versionregexs * @param $useragent * @return string * @author wuzhc 2016-01-22 */ public static function getversion($propertyname,$useragent) { $verregex = array_key_exists($propertyname,self::$versionregexs) ? self::$versionregexs[$propertyname] : null; if(!$verregex) { return ''; } else { $verregex = (array)$verregex; } $match = self::matchversion($verregex,$useragent); //开始匹配 if($match && stripos($propertyname,'window') !== false) { //windown系统版本号需要转换 return self::getwinversion($match); } else { return str_replace('_','.',$match); } } /** * 根据匹配结果转换window系统版本号 * @param $match * @return string * @author wuzhc 2016-01-22 */ protected static function getwinversion($match) { if($match == '6.0') { return 'vista'; } else if($match == '6.1') { return '7'; } else if($match == '6.2') { return '8'; } else if($match == '5.1') { return 'xp'; } } /** * 正则匹配 * @param array $regex * @param $useragent * @return string * @author wuzhc 2016-01-22 */ protected static function match($regex,$useragent) { return (bool)preg_match(sprintf('#%s#is',$regex),$useragent,$matches); } /** * 版本号正则匹配 * @param array $regexs * @param $useragent * @return string * @author wuzhc 2016-01-22 */ protected static function matchversion($regexs,$useragent) { foreach((array)$regexs as $regex) { $regex = str_replace('[ver]','([\w\.]+)', $regex); $match = (bool)preg_match(sprintf('#%s#is',$regex),$useragent,$matches); if($match) { return $matches[1]; } } return ''; }}
其它类似信息

推荐信息