Skip to content

Instantly share code, notes, and snippets.

@lizheming
Created January 29, 2014 11:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lizheming/8685821 to your computer and use it in GitHub Desktop.
Save lizheming/8685821 to your computer and use it in GitHub Desktop.
UA
function user_agent($ua){
//开始解析操作系统
$os = null;
if(preg_match('/Windows 95/i',$ua) || preg_match('/Win95/',$ua)) {
$os = "Windows 95";
} elseif (preg_match('/Windows NT 5.0/i',$ua) || preg_match('/Windows 2000/i', $ua)) {
$os = "Windows 2000";
} elseif (preg_match('/Win 9x 4.90/i',$ua) || preg_match('/Windows ME/i', $ua)) {
$os = "Windows ME";
} elseif (preg_match('/Windows.98/i',$ua) || preg_match('/Win98/i', $ua)) {
$os = "Windows 98";
} elseif (preg_match('/Windows NT 6.0/i',$ua)) {
$os = "Windows Vista";
} elseif (preg_match('/Windows NT 6.1/i',$ua)) {
$os = "Windows 7";
} elseif (preg_match('/Windows NT 5.1/i',$ua)) {
$os = "Windows XP";
} elseif (preg_match('/Windows NT 5.2/i',$ua) && preg_match('/Win64/i',$ua)) {
$os = "Windows XP 64 bit";
} elseif (preg_match('/Windows NT 5.2/i',$ua)){
$os = "Windows Server 2003";
} elseif (preg_match('/Mac_PowerPC/i',$ua)) {
$os = "Mac OS";
} elseif (preg_match('/Windows Phone/i',$ua)) {
$os = "windows phone7";
} elseif (preg_match('/Windows NT 4.0/i',$ua) || preg_match('/WinNT4.0/i',$ua)) {
$os = "Windows NT 4.0";
} elseif (preg_match('/Windows NT/i',$ua) || preg_match('/WinNT/i',$ua)) {
$os = "Windows NT";
} elseif (preg_match('/Windows CE/i',$ua)) {
$os = "Windows CE";
} elseif (preg_match('/ipad/i',$ua)) {
$os = "iPad";
} elseif (preg_match('/Touch/i',$ua)) {
$os = "Touchw";
} elseif (preg_match('/Symbian/i',$ua) || preg_match('/SymbOS/i',$ua)) {
$os = "Symbian OS";
} elseif (preg_match('/PalmOS/i',$ua)) {
$os = "Palm OS";
} elseif (preg_match('/QtEmbedded/i',$ua)) {
$os = "Qtopia";
} elseif (preg_match('/Ubuntu/i',$ua)) {
$os = "Ubuntu Linux";
} elseif (preg_match('/Gentoo/i',$ua)) {
$os = "Gentoo Linux";
} elseif (preg_match('/Fedora/i',$ua)) {
$os = "Fedora Linux";
} elseif (preg_match('/FreeBSD/i',$ua)) {
$os = "FreeBSD";
} elseif (preg_match('/NetBSD/i',$ua)) {
$os = "NetBSD";
} elseif (preg_match('/OpenBSD/i',$ua)) {
$os = "OpenBSD";
} elseif (preg_match('/SunOS/i',$ua)) {
$os = "SunOS";
} elseif (preg_match('/Linux/i',$ua)) {
$os = "Linux";
} elseif (preg_match('/Mac OS X/i',$ua)) {
$os = "Mac OS X";
} elseif (preg_match('/Macintosh/i',$ua)) {
$os = "Mac OS";
} elseif (preg_match('/Unix/i',$ua)) {
$os = "Unix";
} elseif (preg_match('#Nokia([a-zA-Z0-9.]+)#i',$ua,$matches)) {
$os = "Nokia".$matches[1];
} elseif (preg_match('/Mac OS X/i',$ua)) {
$os = "Mac OS X";
} else {
$os = '未知的操作系统';
}
//开始解析浏览器
if(preg_match('#(Camino|Chimera)[ /]([a-zA-Z0-9.]+)#i',$ua,$matches)){
$browser = 'Camino '.$matches[2];
}elseif(preg_match('#SE 2([a-zA-Z0-9.]+)#i',$ua,$matches)){
$browser='搜狗浏览器 2'.$matches[1];
}elseif(preg_match('#360([a-zA-Z0-9.]+)#i',$ua,$matches)){
$browser='360浏览器 '.$matches[1];
}elseif (preg_match('#Maxthon( |\/)([a-zA-Z0-9.]+)#i',$ua,$matches)){
$browser='Maxthon '.$matches[2];
}elseif (preg_match('#Chrome/([a-zA-Z0-9.]+)#i',$ua,$matches)){
$browser='Chrome '.$matches[1];
}elseif (preg_match('#Safari/([a-zA-Z0-9.]+)#i',$ua,$matches)){
$browser='Safari '.$matches[1];
}elseif(preg_match('#opera mini#i', $ua)) {
preg_match('#Opera/([a-zA-Z0-9.]+)#i', $ua, $matches);
$browser='Opera Mini '.$matches[1];
}elseif(preg_match('#Opera.([a-zA-Z0-9.]+)#i',$ua,$matches)){
$browser='Opera '.$matches[1];
}elseif(preg_match('#(j2me|midp)#i', $ua)) {
$browser="J2ME/MIDP Browser";
}elseif(preg_match('/GreenBrowser/i', $ua)){
$browser='GreenBrowser';
}elseif (preg_match('#TencentTraveler ([a-zA-Z0-9.]+)#i',$ua,$matches)){
$browser='腾讯TT浏览器 '.$matches[1];
}elseif(preg_match('#UCWEB([a-zA-Z0-9.]+)#i',$ua,$matches)){
$browser='UCWEB '.$matches[1];
}elseif(preg_match('#MSIE ([a-zA-Z0-9.]+)#i',$ua,$matches)){
$browser='Internet Explorer '.$matches[1];
}elseif(preg_match('#avantbrowser.com#i',$ua)){
$browser='Avant Browser';
}elseif(preg_match('#PHP#', $ua, $matches)){
$browser='PHP';
}elseif(preg_match('#danger hiptop#i',$ua,$matches)){
$browser='Danger HipTop';
}elseif(preg_match('#Shiira[/]([a-zA-Z0-9.]+)#i',$ua,$matches)){
$browser='Shiira '.$matches[1];
}elseif(preg_match('#Dillo[ /]([a-zA-Z0-9.]+)#i',$ua,$matches)){
$browser='Dillo '.$matches[1];
}elseif(preg_match('#Epiphany/([a-zA-Z0-9.]+)#i',$ua,$matches)){
$browser='Epiphany '.$matches[1];
}elseif(preg_match('#UP.Browser/([a-zA-Z0-9.]+)#i',$ua,$matches)){
$browser='Openwave UP.Browser '.$matches[1];
}elseif(preg_match('#DoCoMo/([a-zA-Z0-9.]+)#i',$ua,$matches)){
$browser='DoCoMo '.$matches[1];
}elseif(preg_match('#(Firefox|Phoenix|Firebird|BonEcho|GranParadiso|Minefield|Iceweasel)/([a-zA-Z0-9.]+)#i',$ua,$matches)){
$browser='Firefox '.$matches[2];
}elseif(preg_match('#(SeaMonkey)/([a-zA-Z0-9.]+)#i',$ua,$matches)){
$browser='Mozilla SeaMonkey '.$matches[2];
}elseif(preg_match('#Kazehakase/([a-zA-Z0-9.]+)#i',$ua,$matches)){
$browser='Kazehakase '.$matches[1];
}else{
$browser='未知浏览器';
}
return $os." | ".$browser;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment