note 97088 added to function.get-browser
| From: | Adam at osu1 dot php dot net | Date: | Wed, 31 Mar 2010 20:32:05 +0000 |
| Subject: | note 97088 added to function.get-browser | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-167595@lists.php.net to get a copy of this message | ||
This is a slightly tweaked version of a function that was submitted previously, the previous
function seemed to work ok on firefox and it but not on chrome.
This has been tested to work with FireFox, IE, and Chrome.
function detect_browser( $agent = null ) {
// Declare known browsers to look for
$known = array( 'msie', 'firefox', 'safari', 'webkit',
'opera', 'netscape', 'konqueror', 'gecko',
'chrome' );
/*
* 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'] );
$pattern = '#(?<browser>' . join( '|', $known ) . ')[/
]+(?<version>[0-9]+(?:\.[0-9]+)?)#';
// Find all phrases (or return empty false if none found)
$matches = array();
if( !preg_match_all( $pattern, $agent, $matches ) ) {
return false;
}
// determine which index to use
if( count( $matches['browser'] ) > 1 ) {
$i = 1;
}
else {
$i = 0;
}
// return the results
return array( $matches['browser'][$i] => $matches['version'][$i] );
}
----
Server IP: 66.207.199.35
Probable Submitter: 173.183.49.159
----
Manual Page -- http://www.php.net/manual/en/function.get-browser.php
Edit -- https://master.php.net/note/edit/97088
Del: integrated -- https://master.php.net/note/delete/97088/integrated
Del: useless -- https://master.php.net/note/delete/97088/useless
Del: bad code -- https://master.php.net/note/delete/97088/bad+code
Del: spam -- https://master.php.net/note/delete/97088/spam
Del: non-english -- https://master.php.net/note/delete/97088/non-english
Del: in docs -- https://master.php.net/note/delete/97088/in+docs
Del: other reasons-- https://master.php.net/note/delete/97088
Reject -- https://master.php.net/note/reject/97088
Search -- https://master.php.net/manage/user-notes.php