/    Sign up×
Community /Pin to ProfileBookmark

Regex within a switch Question

I have a simple script, in which I am trying to pull the browser type out of an agent string. This should be horribly easy but I cant seem to pull it off.

I am using regex, and have tried a dizzying array of different configurations. The only response I am getting from the script is the default case, that being unknown. In using my own user agent string, I know the word Firefox is within the string but cant capture it.

[CODE]$agent = $_SERVER[“HTTP_USER_AGENT”];

if (isset($agent)){
switch($agent){
case 1:
if (preg_match(‘/(?:MSIE 3)/’, $agent)){
$browser = “MSIE 3”;

}break;
case 2:
if (preg_match(‘/(?:MSIE 4)/’, $agent)){
$browser = “MSIE 4”;

}break;
case 3:
if (preg_match(‘/(?:MSIE 5)/’, $agent)){
$browser = “MSIE 5”;

} break;
case 4:
if (preg_match(‘/(?:MSIE 6)/’, $agent)){
$browser = “MSIE 6”;

}break;
case 5:
if (preg_match(‘/(?:MSIE 7)/’, $agent)){
$browser = “MSIE 7”;

}break;
case 6:
if (preg_match(‘/?:(MSIE 8)/’, $agent)){
$browser = “MSIE 8”;

} break;
case 7:
if (preg_match(‘/(?:Opera)/’, $agent)){
$browser = “Opera”;

}break;
case 8:
if (preg_match(‘/(?:Chrome)/’, $agent)){
$browser = “Chrome”;

}break;
case 9:
if (preg_match(‘/(?:Safari)/’, $agent)){
$browser = “Safari”;

}break;
case 10:
if (preg_match(‘/^(?:(Firefox))$/’, $agent)){
$browser = “Firefox”;
}break;

default:
$browser = “Unknown”;
}
}[/CODE]

I have tried the break within the if statement and outside. The script from here just goes on to enter the browser into the database.

Any thoughts?

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJul 07.2010 — Looks like a misunderstanding of the "case" part of a switch(). [b]case 1:[/b] is testing if $agent == 1. Since it does not, it goes to the next case, and so on until you reach the default. Also, you don't want to anchor the "Firefox" to the start (^) and end ($) of the regexp. Lastly, by definition $agent is always set since you define it right before you do the isset(). So, here's a shortened piece of code that correctly identified by Firefox browser string:
[code=php]
<?php
$agent = $_SERVER["HTTP_USER_AGENT"];
switch (1) {
case preg_match('/MSIE 3/', $agent):
$browser = "MSIE 3";
break;

case preg_match('/Firefox/', $agent):
$browser = "Firefox";
break;

default:
$browser = "Unknown";
}
echo $browser;
[/code]
Copy linkTweet thisAlerts:
@mrwilsonauthorJul 07.2010 — Ah I see, thank you very much nogdog, as usual you are very helpful. I understand what I was doing wrong
Copy linkTweet thisAlerts:
@NogDogJul 07.2010 — PS: I might approach it from this direction, both as less code and ease of maintenance (just have to change the array to change the list of browsers):
[code=php]
<?php
$agent = $_SERVER["HTTP_USER_AGENT"];
$browsers = array(
'MSIE 3',
'MSIE 4',
'MSIE 5',
'MSIE 6',
'MSIE 7',
'MSIE 8',
'Chrome',
'Safari',
'Opera',
'Firefox'
);
$regexp = '/' . implode('|', $browsers) . '/';
$browser = preg_match($regexp, $agent, $matches) ? $matches[0] : 'unknown';
echo $browser;
[/code]
Copy linkTweet thisAlerts:
@mrwilsonauthorJul 07.2010 — That makes a lot of sense, I will give that a try
×

Success!

Help @mrwilson spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...