note 82981 deleted from function.get-browser by danbrown

From: Date: Tue, 18 May 2010 15:13:02 +0000
Subject: note 82981 deleted from function.get-browser by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-169362@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"]; } } } ?>

« previous php.notes (#169362) next »