Click to See Complete Forum and Search --> : dreaded regexp's


Da Warriah
07-16-2003, 01:21 PM
i just recently learned regexps, and so im having a bit of trouble with them...im trying to make a PHP browser detector, and i have most of it worked out, but im trying to figure out a way to find the difference between netscape and mozilla...heres Netscape's user agent, um, thing:

Mozilla/5.0 (Windows; U; Win 9x 4.90; en-US; rv:1.0.1) Gecko/20020823 Netscape/7.0

and here's Mozilla (Firebird)'s:

Mozilla/5.0 (Windows; U; Win 9x 4.90; en-US; rv:1.4b) Gecko/20030516 Mozilla Firebird/0.6

now how do i use a regexp to determine whether a browser is mozilla or not, either searching for "Mozilla" twice, or making sure "Netscape" and "MSIE" are not listed inside the user agent...thanx for the help:)

Da Warriah
07-17-2003, 08:02 AM
cmon people, does no one know regular expressions or something? all i need is to know how to get Mozilla to return true, and Netscape to return false...

pyro
07-17-2003, 08:16 AM
Just search through looking for the string "Netscape":

<?PHP
$browser = $_SERVER["HTTP_USER_AGENT"];
if (preg_match("/Netscape/",$browser)) {
echo "The browser is Netscape";
}
else {
echo "The browser is not Netscape";
}
?>

pyro
07-17-2003, 08:25 AM
Here's a bit more of a comprehensive browser detection scheme. It works in all the browsers I tested in (IE6, NN7, NN4.7, Mozilla 1.2, and Opera 7)

<?PHP
$browser = $_SERVER["HTTP_USER_AGENT"];
if (preg_match("/MSIE/", $browser)) {
if (preg_match("/Opera/", $browser)) {
$brs = "Opera";
}
else {
$brs = "Internet Explorer";
}
}
else if (preg_match("/Mozilla/", $browser)) {
if (preg_match("/(Netscape|NE)/",$browser)) {
$brs = "Netscape";
}
else {
$brs = "Mozilla";
}
}
else {
$brs = "unknown";
}
echo "The browser is: ".$brs;
?>

Da Warriah
07-17-2003, 12:40 PM
hey thanx, i guess i just didnt think of it that way...i was thinking of differences between the two, when all i needed was the fact that it wasnt one, so it was the other...

*smacks head*

anyways, thanx:D

pyro
07-17-2003, 12:46 PM
lol... No problem... :)