I need 3 regex's that can get the major version number from a user agent string.
I am only interested in IE, Netscape and Firefox
Basically, if the proper regex is used for these 3 browsers, then it should return the version number.
Here is a form to test (It should work fine, but not fully tested):
<?php
if ((isset($_REQUEST['ua']) && ($_REQUEST['ua'] != ""))) {
//Rule for Internet Explorer
if (preg_match('/^[a-zA-Z0-9]
+$/', $_REQUEST['ua'], $matches)) {
$version = $matches[0];
if ((stristr($_REQUEST['ua'],
'msie') && ($version >= 6))) {
$msg = "You have Internet Explorer version: $version";
} else {
$msg = "Your browser version is not supported: $version";
}
}
//Rule for Firefox
elseif (preg_match('/^[a-zA-Z0-9]
+$/', $_REQUEST['ua'], $matches)) {
$version = $matches[0];
if ((stristr($_REQUEST['ua'],
'firefox') && ($version >= 1.5))) {
$msg = "You have Firefox version: $version";
} else {
$msg = "Your browser version is not supported: $version";
}
}
//Rule for Netscape
elseif (preg_match('/^[a-zA-Z0-9]
+$/', $_REQUEST['ua'], $matches)) {
$version = $matches[0];
if ((stristr($_REQUEST['ua'],
'netscape') && ($version >= 8))) {
$msg = "You have Netscape version: $version";
} else {
$msg = "Your broser version is not supported: $version";
}
}
}//end of post
else {
$msg = "Please input a user agent string";
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<p> If I input:<br>
"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1", </p>
<p>$version variable should return an integer of 2 </p>
<p> </p>
<form name="form1" method="post" action="testing8.php">
<p>
<input name="ua" type="text" id="ua" size="120">
</p>
<p><?php if (isset($msg)) { echo $msg; } ?></p>
<p>
<input type="submit" name="Submit" value="Submit">
</p>
</form>
</body>
</html>
Start Free Trial