note 97088 modified in function.get-browser by danbrown
| From: | danbrown@php.net | Date: | Wed, 31 Mar 2010 21:08:06 +0000 |
| Subject: | note 97088 modified in function.get-browser by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-167596@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.
<?php
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] );
}
?>
--was--
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] );
}
http://php.net/manual/en/function.get-browser.php