0

I need to detect if the browser is Android (only from 1 to version 2.3), would this work?

if(preg_match('/Android [1-2.3]/', $_SERVER['HTTP_USER_AGENT'])) {
 echo 'something';
}
MultiformeIngegno
  • 6,959
  • 15
  • 60
  • 119
  • 1
    No. The regex engine can't know how ranges of version numbers work, can it? Also you are misusing the character class, even if it was just a range of numbers you are trying to match. This will match `Android 1`, `Android 2`, `Android .` and `Android 3`. In fact it will match if any of these is a substring of the input (so it would also match `Android 2.5`). Could you show us the actual possible version numbers that could show up in the input? – Martin Ender Dec 24 '12 at 13:26
  • Well, I need to echo a fallback for android versions that don't support SVG (so all the versions < 2.3). – MultiformeIngegno Dec 24 '12 at 13:27
  • That doesn't really help here. A list of "bad" version numbers would. – Martin Ender Dec 24 '12 at 13:29

5 Answers5

0

I don't know really know what the UserAgents for those devices look like, but the pattern you have is not likely to be what you want. I suspect you want something more along the lines of:

'/Android (1\.\d|2\.[012])/'

There are lots of different ways you could write the pattern, but essentially this one says that Android needs to be followed by either 1.{any number} or 2.0, 2.1 or 2.2

Peter O'Callaghan
  • 6,181
  • 3
  • 26
  • 27
0

Following Regex can match exactly Android [1-2.3] .

Android ([1](?:\.[0-9])?|[2](?:\.[0-3])?$)

For MSIE [6-8] Android [1-2.3]

MSIE [6-8] Android ([1](?:\.[0-9])?|[2](?:\.[0-3])?)$

Running regex here

Mansoor Jafar
  • 1,458
  • 3
  • 15
  • 31
0

I would like to suggest you use Browscap for browser detection.

get_browser function doc http://php.net/manual/en/function.get-browser.php

'Actual' browscap .ini file downloads http://tempdownloads.browserscap.com

It should be noted that the project is closed at the moment and further update of browser-database in question.

Usage exapmle in your case:

$browser = get_browser(null, true);
if (array_key_exists('Platform', $browser) && array_key_exists('Platform_Version', $browser) && $browser['Platform'] == 'Android' && $browser['Platform_Version'] <= 2.3) {
    echo 'Android <2.3 version';
}
Leonov Mike
  • 803
  • 6
  • 10
0

You can use the following regex to get the Android browser version, be it in x.x.x format or x.x. I have a regex to determine if the device is using one of the versions I'm supporting (2.3.x +):

/Android ([2].?[3-9].?[0-9]?|[3].?[0-9].?[0-9]?|[4].?[0-9].?[0-9]?)/
ryan508
  • 152
  • 10
0

From github I found this code very useful: https://gist.github.com/Abban/3939726

if(is_old_android('2.3')) {
  echo 'This device has Android 2.3 or lower';
}

function is_old_android($version = '4.0.0'){
   if(strstr($_SERVER['HTTP_USER_AGENT'], 'Android')){

     preg_match('/Android (\d+(?:\.\d+)+)[;)]/', $_SERVER['HTTP_USER_AGENT'], $matches);

     // $matches[0]= Android 4.1.2;
     // $matches[1]= 4.1.2

        return version_compare($matches[1], $version, '<=');
    }
}

Very useful function: version_compare

trante
  • 33,518
  • 47
  • 192
  • 272