note 82982 deleted from function.get-browser by nlopess
| From: | nlopess@php.net | Date: | Mon, 05 May 2008 17:48:01 +0000 |
| Subject: | note 82982 deleted from function.get-browser by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-139079@lists.php.net to get a copy of this message | ||
Note Submitter: The Great God Om
----
Regexes to extract browser name and version info from a user agent string. If it doesn't find
a browser name from the "subclass" list it tries the "superclass" list.
<?
$subclass = "MSIE,Firefox,Konqueror,Safari,Opera,
Netscape,Navigator,Lynx,Amaya,Omniweb";
$superclass = "Gecko,Mozilla,Mosaic,Webkit";
$subclassregex = "(?:" . str_replace(",",")|(?:",$subclass) .
")";
$superclassregex = "(?:" . str_replace(",",")|(?:",$subclass) .
")";
$browser_name = "unrecognized";
$browser_majorversion = 0;
$browser_fullversion = "0.0";
$bsuperclassmatch = FALSE;
$bsubclassmatch = preg_match("/(?<browser>" . $subclassregex .
")(?:\D*)(?<majorversion>\d*)
(?<subversion>(?:\.\d*)*)/i", $_SERVER['HTTP_USER_AGENT'], $matches);
if (!$bsubclassmatch){
$bsuperclassmatch = preg_match("/(?<browser>" . $superclassregex .
")(?:\D*)(?<majorversion>\d*)
(?<subversion>(?:\.\d*)*)/i", $_SERVER['HTTP_USER_AGENT'], $matches);
}
if ($bsubclassmatch||$bsuperclassmatch){
$browser_name = strtolower($matches["browser"]);
$browser_majorversion = (int)$matches["majorversion"];
$browser_fullversion = $matches["majorversion"].$matches["subversion"];
if (!strcasecmp($browser_name, "Safari")){
$safarimatch = preg_match("/Version\/(?<majorversion>\d*)
(?<subversion>(?:\.\d*)*)/i", $_SERVER['HTTP_USER_AGENT'], $safarimatches);
if ($safarimatch){
$browser_majorversion = (int)$safarimatches["majorversion"];
$browser_fullversion =
$safarimatches["majorversion"].$safarimatches["subversion"];
}
}
}
?>