This was my attempt at a similar thing from a while ago, there really is no clean way to do it for a large number of browsers.
function get_platform_info($ua = null){
if ($ua === null){
$ua = $_SERVER['HTTP_USER_AGENT'];
}
$ua = str_replace(array("\r", "\n"), '', strtolower($ua));
$bname = 'Unknown';
$bver = 'Unknown';
$osname = 'Unknown';
$osver = 'Unknown';
// find the OS and version if possible.
if (strpos($ua, 'iphone') !== false || strpos($ua, 'ipad') !== false || strpos($ua, 'ipod') !== false){
$osname = 'iOS';
preg_match('#(?:iphone|ios|os) ([0-9\._]+)#', $ua, $version);
$osver = (empty($version[1])) ? 'Unknown' : str_replace('_', '.', $version[1]);
}else if (strpos($ua, 'android') !== false){
$osname = 'Android';
preg_match('#android ([0-9\.]+)#', $ua, $version);
$osver = (empty($version[1])) ? 'Unknown' : $version[1];
}else if (strpos($ua, 'blackberry') !== false){
$osname = 'BlackBerry';
preg_match('#(?:blackberry[0-9]+|version)/([0-9\.]+)#', $ua, $version);
$osver = (empty($version[1])) ? 'Unknown' : $version[1];
}else if (strpos($ua, 'symbianos') !== false){
$osname = 'SymbianOS';
preg_match('#symbianos/([0-9\.]+)#', $ua, $version[1]);
$osver = (empty($version[1])) ? 'Unknown' : $version[1];
}else if (strpos($ua, 'palmos') !== false || strpos($ua, 'palmsource') !== false){
$osname = 'PalmOS';
preg_match('#palmos ([0-9\.]+)#', $ua, $version);
$osver = (empty($version[1])) ? 'Unknown' : $version[1];
}else if (strpos($ua, 'wm5') !== false){
$osname = 'Windows Mobile';
$osver = '5';
}else if (strpos($ua, 'windows phone') !== false || strpos($ua, 'windows mobile') !== false || strpos($ua, 'mobileexplorer') !== false){
$osname = 'Windows Mobile';
preg_match('#(?:windows (?:phone|mobile) |mobileexplorer/)([0-9\.]+)#', $ua, $version);
$osver = (empty($version[1])) ? 'Unknown' : $version[1];
}else if (strpos($ua, 'kindle') !== false){
$osname = 'Amazon Kindle';
preg_match('#kindle/([0-9\.]+)#', $ua, $version);
$osver = (empty($version[1])) ? 'Unknown' : $version[1];
}else if (strpos($ua, 'webos') !== false){
$osname = 'WebOS';
preg_match('#webos/([0-9\.]+)#', $ua, $version);
$osver = (empty($version[1])) ? 'Unknown' : $version[1];
}else if (strpos($ua, 'benq-') !== false){
$osname = 'BenQ Mobile Device';
}else if (strpos($ua, 'lg-') !== false || strpos($ua, 'lge') !== false){
$osname = 'LG Mobile Device';
}else if (strpos($ua, 'nokia') !== false){
$osname = 'Nokia Mobile Device';
}else if (strpos($ua, 'motorola') !== false || strpos($ua, 'mot') !== false){
$osname = 'Motorola Mobile Device';
}else if (strpos($ua, 'nintendo wii') !== false){
$osname = 'Nintendo Wii Console';
}else if (strpos($ua, 'o2 xda') !== false){
$osname = 'o2 Mobile Device';
}else if (strpos($ua, 'samsung') !== false || strpos($ua, 'sec') !== false){
$osname = 'Samsung Mobile Device';
}else if (strpos($ua, 'sanyo') !== false){
$osname = 'Sanyo Mobile Device';
}else if (strpos($ua, 'sharp') !== false){
$osname = 'Sharp Mobile Device';
}else if (strpos($ua, 'sie-') !== false){
$osname = 'Siemens Mobile Device';
}else if (strpos($ua, 'sonyericsson') !== false){
$osname = 'Sony/Ericsson Mobile Device';
}else if (strpos($ua, 'playstation portable') !== false || strpos($ua, 'psp') !== false){
$osname = 'Sony PSP';
}else if (strpos($ua, 'playstation') !== false){
$osname = 'Sony Playstation';
}else if (strpos($ua, 'linux') !== false || strpos($ua, 'x11') !== false){
if (strpos($ua, 'ubuntu') !== false){
$osname = 'Ubuntu';
$regexp = '#ubuntu/([0-9\.]+)#';
}else if (strpos($ua, 'mint') !== false){
$osname = 'Linux Mint';
$regexp = '#mint/([0-9\.]+)#';
}else if (strpos($ua, 'debian') !== false){
$osname = 'Debian';
$regexp = '#debian/([0-9\.]+)#';
}else if (strpos($ua, 'knoppix') !== false){
$osname = 'Knoppix';
$regexp = '#knoppix/([0-9\.]+)#';
}else if (strpos($ua, 'kubuntu') !== false){
$osname = 'Kubuntu';
$regexp = '#ubuntu/([0-9\.]+)#';
}else if (strpos($ua, 'xubuntu') !== false){
$osname = 'Xubuntu';
$regexp = '#ubuntu/([0-9\.]+)#';
}else if (strpos($ua, 'fedora') !== false){
$osname = 'Fedora';
$regexp = '#fedora/([0-9\.]+)#';
}else if (strpos($ua, 'centos') !== false){
$osname = 'centOS';
$regexp = '#centos/([0-9\.]+)#';
}else if (strpos($ua, 'mandriva') !== false){
$osname = 'Mandriva';
$regexp = '#mandriva/([0-9\.]+)#';
}else if (strpos($ua, 'redhat') !== false || strpos($ua, 'red_hat') !== false || strpos($ua, 'red-hat') !== false || strpos($ua, 'sunos') !== false){
$osname = 'Red Hat';
$regexp = '#(?:redhat|red_hat|red-hat|sunos)/([0-9\.]+)#';
}else if (strpos($ua, 'suse') !== false){
$osname = 'openSUSE';
$regexp = '#suse/([0-9\.]+)#';
}else if (strpos($ua, 'slackware') !== false){
$osname = 'Slackware';
$regexp = '#slackware/([0-9\.]+)#';
}else if (strpos($ua, 'backtrack') !== false){
$osname = 'Backtrack';
$regexp = '#backtrack/([0-9\.]+)#';
}else if (strpos($ua, 'gentoo') !== false){
$osname = 'Gentoo';
$regexp = '#gentoo/([0-9\.]+)#';
}else{
$osname = 'Linux';
}
if (isset($regexp)){
preg_match($regexp, $ua, $version);
}
$osver = (empty($version[1])) ? 'Unknown' : $version[1];
}else if (strpos($ua, 'win') !== false){
$osname = 'Windows';
if (strpos($ua, 'windows 95') !== false || strpos($ua, 'windows_95') !== false || strpos($ua, 'win95') !== false){
$osver = '95';
}else if (strpos($ua, 'windows 98') !== false || strpos($ua, 'win98') !== false){
$osver = '98';
}else if (strpos($ua, 'windows nt 5.0') !== false || strpos($ua, 'windows 2000') !== false){
$osver = '2000';
}else if (strpos($ua, 'windows nt 5.1') !== false || strpos($ua, 'windows xp') !== false){
$osver = 'XP';
}else if (strpos($ua, 'windows nt 5.2') !== false){
$osver = 'Server 2003';
}else if (strpos($ua, 'windows nt 6.0') !== false){
$osver = 'Vista';
}else if (strpos($ua, 'windows nt 6.1') !== false){
$osver = '7';
}else if (strpos($ua, 'windows me') !== false){
$osver = 'ME';
}else if (strpos($ua, 'windows ce') !== false){
$osver = 'CE';
}else if (strpos($ua, 'windows nt') !== false || strpos($ua, 'winnt') !== false){
$osver = 'NT 4.0';
}
}else if (strpos($ua, 'macintosh') !== false || strpos($ua, 'mac os') !== false){
if (strpos($ua, 'mac os x') !== false){
$osname = 'Mac OS X';
}else{
$osname = 'Mac OS';
}
preg_match('#mac os x ([0-9\._]+)#', $ua, $version);
$osver = (empty($version[1])) ? 'Unknown' : str_replace('_', '.', $version[1]);
}
// because we need to check for these again later.
unset($regexp, $version);
// find the browser and version if possible.
// list of browser user agents from http://www.zytrax.com/tech/web/browser_ids.htm
if (strpos($ua, 'amaya') !== false){
$bname = 'Amaya';
$regexp = '#amaya/([0-9\.]+)#';
}else if (strpos($ua, 'amigavoyager') !== false){
$bname = 'Amiga Voyager';
$regexp = '#amigavoyager/([0-9\.]+)#';
}else if (strpos($ua, 'xchaos_arachne') !== false){
$bname = 'Arachne';
$regexp = '#xchaos_arachne/([0-9\.]+)#';
}else if (strpos($ua, 'apt-http') !== false){
$bname = 'apt-get';
$regexp = '#apt-http/([0-9\.]+)#';
}else if (strpos($ua, 'arora') !== false){
$bname = 'Arora';
$regexp = '#arora/([0-9\.]+)#';
}else if (strpos($ua, 'avant browser') !== false){
$bname = 'Avant Browser';
}else if (strpos($ua, 'amiga-aweb') !== false){
$bname = 'Amiga Aweb';
$regexp = '#amiga-aweb/([0-9\.]+)#';
}else if (strpos($ua, 'browsex') !== false){
$bname = 'BrowseX';
$regexp = '#browsex \(([0-9\.]+)#';
}else if (strpos($ua, 'camino') !== false){
$bname = 'Camino';
$regexp = '#camino/([0-9\.]+)#';
}else if (strpos($ua, 'charon') !== false){
$bname = 'Charon';
}else if (strpos($ua, 'check&get') !== false){
$bname = 'Check&Get';
$regexp = '#check&get ([0-9\.]+)#';
}else if (strpos($ua, 'chimera') !== false){
$bname = 'Chimera';
$regexp = '#chimera/([0-9\.]+)#';
}else if (strpos($ua, 'amaya') !== false){
$bname = 'Amaya';
$regexp = '#amaya/([0-9\.]+)#';
}else if (strpos($ua, 'comodo_dragon') !== false){
$bname = 'Comodo Dragon';
$regexp = '#dragon/([0-9\.]+)#';
}else if (strpos($ua, ' iron') !== false){
$bname = 'SRWare Iron';
$regexp = '#iron/([0-9\.]+)#';
}else if (strpos($ua, 'like gecko) chrome') !== false){
$bname = 'Google Chrome';
$regexp = '#chrome/([0-9\.]+)#';
}else if (strpos($ua, 'curl') !== false){
$bname = 'cURL';
$regexp = '#curl/([0-9\.]+)#';
}else if (strpos($ua, 'democracy') !== false){
$bname = 'Democracy';
$regexp = '#democracy/([0-9\.]+)#';
}else if (strpos($ua, 'dillo') !== false){
$bname = 'Dillo';
$regexp = '#dillo/([0-9\.]+)#';
}else if (strpos($ua, 'doczilla') !== false){
$bname = 'DocZilla';
$regexp = '#doczilla/([0-9\.]+)#';
}else if (strpos($ua, 'edbrowse') !== false){
$bname = 'edbrowse';
$regexp = '#edbrowse/([0-9\.]+)#';
}else if (strpos($ua, 'elinks') !== false){
$bname = 'ELinks';
$regexp = '#elinks/([0-9\.]+)#';
}else if (strpos($ua, 'emacs-w3') !== false){
$bname = 'Emacs/w3s';
$regexp = '#emacs-w3/([0-9\.]+)#';
}else if (strpos($ua, 'epiphany') !== false){
$bname = 'Epiphany';
$regexp = '#epiphany/([0-9\.]+)#';
}else if (strpos($ua, 'fetch libfetch') !== false){
$bname = 'Fetch';
}else if (strpos($ua, 'fennec') !== false){
$bname = 'Mobile Firefox';
$regexp = '#fennec/([0-9\.]+)#';
}else if (strpos($ua, 'flock') !== false){
$bname = 'Flock';
$regexp = '#flock/([0-9\.]+)#';
}else if (strpos($ua, 'galeon') !== false){
$bname = 'Galeon';
$regexp = '#galeon/([0-9\.]+)#';
}else if (strpos($ua, 'iceweasel') !== false){
$bname = 'IceWeasel';
$regexp = '#iceweasel/([0-9\.]+)#';
}else if (strpos($ua, 'iseape') !== false){
$bname = 'IceApe';
$regexp = '#iceape/([0-9\.]+)#';
}else if (strpos($ua, 'icecat') !== false){
$bname = 'IceCat';
$regexp = '#icecat/([0-9\.]+)#';
}else if (strpos($ua, 'palemoon') !== false){
$bname = 'Palemoon';
$regexp = '#palemoon/([0-9\.]+)#';
}else if (strpos($ua, 'swiftfox') !== false){
$bname = 'SwiftFox';
$regexp = '#firefox/([0-9\.]+)#';
}else if (strpos($ua, 'firefox') !== false){
$bname = 'Firefox';
$regexp = '#firefox/([0-9\.]+)#';
}else if (strpos($ua, 'superswan') !== false){
$bname = 'Firefox';
$regexp = '#superswan/([0-9\.]+)#';
}else if (strpos($ua, 'deepnet explorer') !== false){
$bname = 'GreenBrowser';
$regexp = '#deepnet explorer ([0-9\.]+)#';
}else if (strpos($ua, 'ibrowse') !== false){
$bname = 'IBrowse';
$regexp = '#ibrowse/([0-9\.]+)#';
}else if (strpos($ua, 'icab') !== false){
$bname = 'iCab';
$regexp = '#icab(?:/| )([0-9\.]+)#';
}else if (strpos($ua, 'alertsite.com') !== false){
$bname = 'AlertSite.com';
$regexp = '#ipd/([0-9\.]+)#';
}else if (strpos($ua, 'ice browser') !== false){
$bname = 'ICE';
$regexp = '#browser/([0-9\.]+)#';
}else if (strpos($ua, 'kazehakase') !== false){
$bname = 'Kazehakase';
$regexp = '#kazehakase/([0-9\.]+)#';
}else if (strpos($ua, 'kkman') !== false){
$bname = 'Kkman';
$regexp = '#kkman([0-9\.]+)#';
}else if (strpos($ua, 'k-melon') !== false){
$bname = 'K-Melon';
$regexp = '#(?:k\-melon |k-melon/|k-melonccfme )([0-9\.]+)#';
}else if (strpos($ua, 'konqueror') !== false){
$bname = 'Konqueror';
$regexp = '#konqueror/([0-9\.]+)#';
}else if (strpos($ua, 'lobo') !== false){
$bname = 'Lobo';
$regexp = '#lobo/([0-9\.]+)#';
}else if (strpos($ua, 'lynx') !== false){
$bname = 'Lynx';
$regexp = '#lynx/([0-9\.]+)#';
}else if (strpos($ua, 'maxathon') !== false){
$bname = 'Maxathon';
}else if (strpos($ua, 'maxthon') !== false){
$bname = 'Maxthon';
$regexp = '#maxthon/([0-9\.]+)#';
}else if (strpos($ua, 'midori') !== false){
$bname = 'midori';
$regexp = '#midori/([0-9\.]+)#';
}else if (strpos($ua, 'mosaic') !== false){
$bname = 'Mosaic';
$regexp = '#(mosaic|digital)/([0-9\.]+)#';
}else if (strpos($ua, 'seamonkey') !== false){
$bname = 'Mozilla (SeaMonkey)';
$regexp = '#seamonkey/([0-9\.]+)#';
}else if (strpos($ua, 'crazy browser') !== false){
$bname = 'Crazy Browser';
$regexp = '#crazy browser ([0-9\.]+)#';
}else if (strpos($ua, 'theworld') !== false){
$bname = 'TheWorld';
}else if (strpos($ua, 'bsalsa') !== false){
$bname = 'bsalsa';
}else if (strpos($ua, 'sleipnir') !== false){
$bname = 'Sleipnir';
$regexp = '#sleipnir/([0-9\.]+)#';
}else if (strpos($ua, 'slimbrowser') !== false){
$bname = 'SlimBrowser';
}else if (strpos($ua, 'mucommander') !== false){
$bname = 'muCommander';
$regexp = '#mucommander v([0-9\.]+)#';
}else if (strpos($ua, 'netpositive') !== false){
$bname = 'NetPositive';
$regexp = '#netpositive/([0-9\.]+)#';
}else if (strpos($ua, 'navigator') !== false){
$bname = 'Netscape Navigator';
$regexp = '#navigator/([0-9\.]+)#';
}else if (strpos($ua, 'netscape') !== false){
$bname = 'Netscape Navigator';
$regexp = '#(?:netscape|netscape6)/([0-9\.]+)#';
}else if (strpos($ua, 'netsurf') !== false){
$bname = 'Netsurf';
$regexp = '#netsurf/([0-9\.]+)#';
}else if (strpos($ua, 'offbyone') !== false){
$bname = 'OffByOne';
}else if (strpos($ua, 'omniweb') !== false){
$bname = 'Omniweb';
$regexp = '#omniweb(?:/v|/)([0-9\.]+)#';
}else if (strpos($ua, 'opera mini') !== false){
$bname = 'Opera Mini';
$regexp = '#opera mini(?:/| )([0-9\.]+)#';
}else if (strpos($ua, 'opera') !== false){
$bname = 'Opera';
$regexp = '#version(?:/| )([0-9\.]+)#';
}else if (strpos($ua, 'printsmart') !== false){
$bname = 'HP Web PrintSmart';
$regexp = '#printsmart (?:[a-z0-9]+) ([0-9\.]+)#';
}else if (strpos($ua, 'prism') !== false){
$bname = 'Mozilla Prism';
$regexp = '#prism/([0-9\.]+)#';
}else if (strpos($ua, 'retawq') !== false){
$bname = 'retawq';
$regexp = '#retawq/([0-9\.]+)#';
}else if (strpos($ua, 'shiira') !== false){
$bname = 'Shirra';
}else if (strpos($ua, ' sis ') !== false){
$bname = 'Spectrum Internet Suite';
$regexp = '#sis /([0-9\.]+)#';
}else if (strpos($ua, 'spicebird') !== false){
$bname = 'Spicebird';
$regexp = '#spicebird/([0-9\.]+)#';
}else if (strpos($ua, 'songbird') !== false){
$bname = 'Songbird';
$regexp = '#songbird/([0-9\.]+)#';
}else if (strpos($ua, 'stainless') !== false){
$bname = 'Stainless';
$regexp = '#stainless/([0-9\.]+)#';
}else if (strpos($ua, 'strata') !== false){
$bname = 'Strata';
$regexp = '#strata/([0-9\.]+)#';
}else if (strpos($ua, 'sylera') !== false){
$bname = 'Sylera';
$regexp = '#sylera/([0-9\.]+)#';
}else if (strpos($ua, 'w3clinemode') !== false){
$bname = 'W2C Line Mode';
$regexp = '#w3clinemode/([0-9\.]+)#';
}else if (strpos($ua, 'w3m') !== false){
$bname = 'w3m';
$regexp = '#w3m/([0-9\.]+)#';
}else if (strpos($ua, 'wget') !== false){
$bname = 'Wget';
$regexp = '#wget/([0-9\.]+)#';
}else if (strpos($ua, 'xenu_link_sleuth') !== false){
$bname = 'Xenu\'s Link Analyser';
$regexp = '#xenu_link_sleuth_([0-9\.]+)#';
}else if (strpos($ua, 'netmath') !== false){
$bname = 'Maxima';
}else if (strpos($ua, 'avantgo') !== false){
$bname = 'AvantGo';
$regexp = '#avantgo ([0-9\.]+)#';
}else if (strpos($ua, 'docomo') !== false){
$bname = 'DoCoMo (NTT Mobile)';
$regexp = '#docomo/([0-9\.]+)#';
}else if (strpos($ua, 'eudoraweb') !== false){
$bname = 'EudoraWeb';
$regexp = '#eudoraweb ([0-9\.]+)#';
}else if (strpos($ua, 'minimo') !== false){
$bname = 'Minimo';
$regexp = '#minimo/([0-9\.]+)#';
}else if (strpos($ua, 'plink') !== false){
$bname = 'PocketLink';
$regexp = '#plink ([0-9\.]+)#';
}else if (strpos($ua, 'netfront') !== false){
$bname = 'NetFront';
$regexp = '#netfront(?:/| )([0-9\.]+)#';
}else if (strpos($ua, 'pie') !== false){
$bname = 'Pocket Internet Explorer';
}else if (strpos($ua, 'xiino') !== false){
$bname = 'Xiino';
$regexp = '#xiino/([0-9\.]+)#';
}else if (strpos($ua, 'iemobile') !== false){
$bname = 'Internet Explorer Mobile';
$regexp = '#iemobile ([0-9\.]+)#';
}else if (strpos($ua, 'elaine') !== false){
$bname = 'PalmPilot';
}else if (strpos($ua, 'msie') !== false){
$bname = 'Internet Explorer';
$regexp = '#msie ([0-9\.]+)#';
}else if (strpos($ua, 'safari') !== false){
if (strpos($ua, 'mobile') !== false){
$bname = 'Mobile Safari';
}else{
$bname = 'Safari';
}
$regexp = '#(?:version|safari)/([0-9\.]+)#';
}else if (strpos($ua, 'wdg_validator') !== false){
$bname = 'WDG Validator';
$regexp = '#wdg_validator/([0-9\.]+)#';
}else if (strpos($ua, 'w3c_validator') !== false){
$bname = 'W3C Validator';
$regexp = '#w3c_validator/([0-9\.]+)#';
}else if (strpos($ua, 'bobby') !== false){
$bname = 'Bobby (validator)';
$regexp = '#bobby/([0-9\.]+)#';
}else if (strpos($ua, 'w3c_css_validator') !== false){
$bname = 'W3C CSS Validator';
$regexp = '#w3c_css_validator_jfouffa/([0-9\.]+)#';
}else if (strpos($ua, 'httpclient') !== false){
$bname = 'HTTPClient';
$regexp = '#httpclient/([0-9\.]+)#';
}
if (isset($regexp)){
preg_match($regexp, $ua, $version);
}
$bver = (empty($version[1])) ? 'Unknown' : $version[1];
return array(
'browser' => array(
'name' => $bname,
'version' => $bver
),
'platform' => array(
'name' => $osname,
'version' => $osver
)
);
}