More mobile browser detection testing

My standard method of mobile browser detection as detailed in my previous post here involves the PHP function preg_match(). Unfortunately for me, having compiled a stripped down version PHP to run on ARM linux, the preg_match() function was not included (probably because I used the disable-all switch during compilation).

Anyhow I was quite quickly able to come up with a workaround using strpos():

//Read the user agent string, and convert to lowercase
$useragent=strtolower($_SERVER['HTTP_USER_AGENT']);


//Array of mobile browsers
$mobile_browser=array('android','avantgo','blackberry','blazer','compal','elaine','fennec','hiptop','iemobile','iphone','ipod','iris','kindle','lge ','maemo','midp','mmp','opera mobi','opera mini','palm os','phone','pixi','pre','plucker','pocket','psp','symbian','treo','vodafone','wap','windows ce','windows phone','xda','xiino/i');


//initialise counter
$i=0;


//iterate thorough array
foreach($mobile_browser as $val) {
$pos = strpos($useragent, $val);
//if the array element is not in the user agent string, do nothing
if($pos === false) {
}
//else increment the counter
else {
$i++;
}
}
if ($i == 0) {
echo "Load normal site";
}
else {
echo "Load mobile site";
}

I also found a great way to test all mobile browsers, using the user agent switcher for Firefox.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: