note 95195 deleted from function.get-browser by danbrown

From: Date: Thu, 17 Dec 2009 15:55:10 +0000
Subject: note 95195 deleted from function.get-browser by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-164093@lists.php.net to get a copy of this message
Note Submitter: del at NOSPAMdoofer dot org ---- To extend the function that 'robert at broofa dot com' gave, I wanted to also get the platform too, which can be helpful for certain things (like ActiveX). So I modified the function thusly: function browserInfo($agent=null) { $results = array(); // Declare known browsers to look for $browsers = array('msie', 'firefox', 'safari', 'webkit', 'opera', 'netscape', 'konqueror', 'gecko'); // Declare known platforms to look for $platforms = array('linux', 'windows', 'macppc', 'mac68k'); // Clean up agent and build regex that matches phrases for known browsers // (e.g. "Firefox/2.0" or "MSIE 6.0" (This only matches the major and minor // version numbers. E.g. "2.0.0.6" is parsed as simply "2.0" $agent = strtolower($agent ? $agent : $_SERVER['HTTP_USER_AGENT']); $browserPattern = '#(?<browser>' . join('|', $browsers) . ')[/ ]+(?<version>[0-9]+(?:\.[0-9]+)?)#'; $platformPattern = '#(?<platform>' . join('|', $platforms) . '?)#'; // Find all phrases (or return empty array if none found) if (preg_match_all($browserPattern, $agent, $matches)) { // Since some UAs have more than one phrase (e.g Firefox has a Gecko phrase, // Opera 7,8 have a MSIE phrase), use the last one found (the right-most one // in the UA). That's usually the most correct. $i = count($matches['browser'])-1; $results = array($matches['browser'][$i] => $matches['version'][$i]); $results['browser'] = $matches['browser'][$i]; } if (preg_match_all($platformPattern, $agent, $matches)) { $results['platform'] = $matches['platform'][0]; } return $results; }

« previous php.notes (#164093) next »