为了账号安全,请及时绑定邮箱和手机立即绑定

如何印出 macOS 的版本?甚至作業系統名字?

如何印出 macOS 的版本?甚至作業系統名字?

PHP
森林海 2019-03-17 23:07:19
使用 $_SERVER['HTTP_USER_AGENT'] 後印出: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36 在網上找到 function os_info($uagent) { // the order of this array is important global $uagent; $oses = array( 'Win311' => 'Win16', 'Win95' => '(Windows 95)|(Win95)|(Windows_95)', 'WinME' => '(Windows 98)|(Win 9x 4.90)|(Windows ME)', 'Win98' => '(Windows 98)|(Win98)', 'Win2000' => '(Windows NT 5.0)|(Windows 2000)', 'WinXP' => '(Windows NT 5.1)|(Windows XP)', 'WinServer2003' => '(Windows NT 5.2)', 'WinVista' => '(Windows NT 6.0)', 'Windows 7' => '(Windows NT 6.1)', 'Windows 8' => '(Windows NT 6.2)', 'Windows 8.1' => '(Windows NT 6.3)', 'Windows 10' => '(Windows NT 10.0)', 'WinNT' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)', 'OpenBSD' => 'OpenBSD', 'SunOS' => 'SunOS', 'Ubuntu' => 'Ubuntu', 'Android' => 'Android', 'Linux' => '(Linux)|(X11)', 'iPhone' => 'iPhone', 'iPad' => 'iPad', 'MacOS' => '(Mac_PowerPC)|(Macintosh)', 'QNX' => 'QNX', 'BeOS' => 'BeOS', 'OS2' => 'OS/2', 'SearchBot' => '(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)' ); $uagent = strtolower($uagent ? $uagent : $_SERVER['HTTP_USER_AGENT']); foreach ($oses as $os => $pattern) if (preg_match('/' . $pattern . '/i', $uagent)) return $os; return 'Unknown'; } echo os_info($uagent); 這個可以印出 「MacOS」但假設我想印出後面的版本「 10_10_5 」能用什麼代碼做到?因為長度會不一定
查看完整描述

3 回答

?
回首忆惘然

TA贡献1847条经验 获得超11个赞

正则

/Mac OS X ([\d\_]+)/
查看完整回答
反对 回复 2019-03-18
?
海绵宝宝撒

TA贡献1809条经验 获得超8个赞

可以试试get_browser,写成:

<?php
$osInfo = get_browser(null, true);
var_export($osInfo);

不过这个API会依赖php.ini中的browscap配置,它指向的browscap.ini文件可以提供具体uA与操作系统间的映射关系;最新的版本可以去这里看看。

查看完整回答
反对 回复 2019-03-18
  • 3 回答
  • 0 关注
  • 410 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信