正規表現でUser Agent分岐 OS版
User AgentをPHPで分岐し端末(Windows/Mac/Linux/iOS/Android)判別
PHP
/**
*
* 正規表現でUserAgent分岐/OS
*
* @param string $user_agent
* @return string
*/
function getOs($user_agent = '')
{
if (empty($user_agent)) {
// ユーザエージェント
$user_agent = $_SERVER['HTTP_USER_AGENT'];
}
if (preg_match('/Windows NT 10.0/', $user_agent)) {
$os = 'Windows 10';
} elseif (preg_match('/Windows NT 6.3/', $user_agent)) {
$os = 'Windows 8.1 / Windows Server 2012 R2';
} elseif (preg_match('/Windows NT 6.2/', $user_agent)) {
$os = 'Windows 8 / Windows Server 2012';
} elseif (preg_match('/Windows NT 6.1/', $user_agent)) {
$os = 'Windows 7 / Windows Server 2008 R2';
} elseif (preg_match('/Windows NT 6.0/', $user_agent)) {
$os = 'Windows Vista / Windows Server 2008';
} elseif (preg_match('/Windows NT 5.2/', $user_agent)) {
$os = 'Windows XP x64 Edition / Windows Server 2003';
} elseif (preg_match('/Windows NT 5.1/', $user_agent)) {
$os = 'Windows XP';
} elseif (preg_match('/Windows NT 5.0/', $user_agent)) {
$os = 'Windows 2000';
} elseif (preg_match('/Windows NT 4.0/', $user_agent)) {
$os = 'Microsoft Windows NT 4.0';
} elseif (preg_match('/Mac OS X ([0-9\._]+)/', $user_agent, $matches)) {
$os = 'Macintosh Intel ' . str_replace('_', '.', $matches[1]);
} elseif (preg_match('/OS ([a-z0-9_]+)/', $user_agent, $matches)) {
$os = 'iOS ' . str_replace('_', '.', $matches[1]);
} elseif (preg_match('/Android ([a-z0-9\.]+)/', $user_agent, $matches)) {
$os = 'Android ' . $matches[1];
} elseif (preg_match('/Linux ([a-z0-9_]+)/', $user_agent, $matches)) {
$os = 'Linux ' . $matches[1];
} else {
$os = 'unidentified';
}
return $os;
}
UserAgent一覧はphp.o0o0.jp-UserAgent を参照。