note 95195 added to function.get-browser
| From: | del at NOSPAMdoofer dot org | Date: | Thu, 17 Dec 2009 15:50:55 +0000 |
| Subject: | note 95195 added to function.get-browser | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-164092@lists.php.net to get a copy of this message | ||
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;
}
----
Server IP: 69.147.83.197
Probable Submitter: 149.165.236.76
----
Manual Page -- http://www.php.net/manual/en/function.get-browser.php
Edit -- https://master.php.net/note/edit/95195
Del: integrated -- https://master.php.net/note/delete/95195/integrated
Del: useless -- https://master.php.net/note/delete/95195/useless
Del: bad code -- https://master.php.net/note/delete/95195/bad+code
Del: spam -- https://master.php.net/note/delete/95195/spam
Del: non-english -- https://master.php.net/note/delete/95195/non-english
Del: in docs -- https://master.php.net/note/delete/95195/in+docs
Del: other reasons-- https://master.php.net/note/delete/95195
Reject -- https://master.php.net/note/reject/95195
Search -- https://master.php.net/manage/user-notes.php