Space Cat, Prince Among Thieves

PHP User Agent Parser

Demo You are running CCBot version 2.0 on
CCBot/2.0 (
Try It!


  • PHP 5.3.0+


The script is availible over on GitHub or via Composer:

    "require": {
        "donatj/phpuseragentparser": "*"

Sample Usage

$ua_info = parse_user_agent();
    'platform' => '[Detected Platform]',
    'browser'  => '[Detected Browser]',
    'version'  => '[Detected Browser Version]',

Currently Detected Platforms

  • Desktop
    • Windows
    • Linux
    • Macintosh
    • Chrome OS
  • Mobile
    • Android
    • iPhone
    • iPad / iPod Touch
    • Windows Phone OS
    • Kindle
    • Kindle Fire
    • BlackBerry
    • Playbook
    • Tizen
  • Console
    • Nintendo 3DS
    • New Nintendo 3DS
    • Nintendo Wii
    • Nintendo WiiU
    • PlayStation 3
    • PlayStation 4
    • PlayStation Vita
    • Xbox 360
    • Xbox One

Currently Detected Browsers

  • Android Browser
  • BlackBerry Browser
  • Camino
  • Kindle / Silk
  • Firefox / IceWeasel / IceCat
  • Safari
  • Internet Explorer
  • IEMobile
  • Chrome / HeadlessChrome
  • Yandex Browser
  • Opera
  • Midori
  • Vivaldi
  • TizenBrowser
  • SamsungBrowser
  • UC Browser
  • Lynx
  • Wget
  • Curl
  • Puffin

Comment by: ling on

ling GravatarThank you very much !!

Comment by: dankoi on

Thank you so much!
Great job!

Comment by: Valery on

Valery GravatarCan you add platform version support?
For example WinXP, Win7, Win8, etc.
And processor detail 32/64 bit.

Comment by: RuuRd on

RuuRd Gravatarthanks Donat for your great parse_user_agent function! I read through your code and I have a few questions. as far as I know array_intersect never returns a boolean .....
if( $keys = array_intersect($priority, $result['platform']) ) {
assignment to $key instead of evaluation:
if( ( $data['platform'] == 'Android' && !($key = 0) ) ||
some $key = array_search() evaluation to “!== false” seem to be missing .. like
elseif( $key = array_search( 'Safari', $result['browser'] ) ) {
Your function works well though!

Comment by: Jesse G. Donat on

Jesse G. Donat GravatarPHP uses implicit type conversion, whereas an empty array is false-y and one containing any value is truth-y, and my code is simply taking advantage of this feature of the language to save on code.

Comment by: sachz on

sachz Gravatarthank you for this useful script (:

Comment by: Tomas on

  • Resolved
Tomas GravatarThanks for this. But it throws error when I try to validate my site with

[2013-07-09 23:39:06] Notice: 8. Message: Undefined offset: 0. In file Request.class.php at line 241. Browser: v (Unknown)
[2013-07-09 23:39:06] Notice: 8. Message: Undefined offset: 0. In file Request.class.php at line 242. Browser: v (Unknown)
[2013-07-09 23:42:30] Notice: 8. Message: Undefined offset: 0. In file Request.class.php at line 257. Browser: v (Unknown)
[2013-07-09 23:42:30] Notice: 8. Message: Undefined offset: 0. In file Request.class.php at line 275. Browser: v (Unknown)

first two errors:
$data['browser'] = $result['browser'][0];
$data['version'] = $result['version'][0];

and others in:

} elseif($result['browser'][0] == 'AppleWebKit') {

} elseif($result['browser'][0] == 'MSIE') {

Comment by: Jesse G. Donat on

Jesse G. Donat GravatarIdeally you should not be running with notices turned on on production. I will look into fixing these, but notice free code in PHP is very rare.

Considering hiding notices using

Comment by: farzin on

farzin Gravatar

hi , i have failed to use your script. i have uploaded the whole github directory. Then i'm running UserAgentParserTest.php and i get nothing.

i use this code in another php and i get nothing again :(

require 'UserAgentParser.php';
$ua_info = parse_user_agent();
echo "farzin".$ua_info;

Comment by: farzin on

farzin Gravatar

i finally got it working by adding this codes and its working fine now . thank you for sharing this. :


echo $agent.'<br />';

echo '<h1>Test Suite</h1>';

$parsedagent = parse_user_agent($agent);

echo "<pre>";
echo "</pre>";

Comment by: dac on

dac GravatarHey, very nice script, but to be more precise on distinguish Opera and Opera Next, just add one more elseif rather than using OR when checking 'OPR' || 'Opera'. IF 'OPR' then it is Opera Next, IF 'Opera' then it is Opera. This is only PHP script that scrapes right version of Opera, weather it is Opera Next or Opera. It is not a bug, but it is more precise. :)

Comment by: ~BoogL~ on

  • Resolved
~BoogL~ GravatarMay be i'm wrong, but i think it's wrong detection on this agents:

Mozilla/5.0 (Linux; Android 4.0.4; GT-P5100 Build/IMM76D) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.90 Safari/537.36
User Agent Returned: Chrome version 537.36 on Android but not version 27.0.1453.90

Opera/9.80 (J2ME/MIDP; Opera Mini/4.4.28000/30.3061; U; ru) Presto/2.8.119 Version/11.10
User Agent Returned: Opera version 11.10 but not Opera Mini 4.4.28000

And i'm not shure that Mozilla/5.0 (Linux; U; Android 4.1.1; ru-ru; A701 Build/JRO03H) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Safari/534.30
is Chrome version 4.0

Comment by: Jesse G. Donat on

Jesse G. Donat GravatarYou are right, I have corrected this in the latest release v0.1.5 and included a test specifically for that UA string.

Comment by: Oleksiy on

Oleksiy GravatarGreat job!
Best for today!

Comment by: Mark on

Mark GravatarMozilla/5.0 (Linux; Android 4.2.2; de-at; SAMSUNG GT-I9195/I9195XXUAMF6 Build/JDQ39) AppleWebKit/535.19 (KHTML, like Gecko) Version/1.0 Chrome/18.0.1025.308 Mobile Safari/535.19

Here is a problem... it returns Android Version 1.0

Comment by: Kalla on

Kalla GravatarCode is working fine. But how i get complete platform ex. Windows XP, Windows Vista, Windows 7 or Windows 8

Comment by: Varuzhan Kankanyan on

Varuzhan Kankanyan Gravatar

Hello, I think will be reasonable to add an option to return also device type ( Desktop, Mobile, Console). Bellow the code to be added:

$platforms = array(
    'desktop' => array('Windows','Linux','Macintosh','Chrome OS'),
    'mobile' => array('Android','iPhone','iPad','Windows Phone OS','Kindle','Kindle Fire','BlackBerry','Playbook'),
    'console' => array('Nintendo 3DS','Nintendo Wii','Nintendo WiiU','PlayStation 3','PlayStation Vita','Xbox 360')

foreach($platforms as $k=>$v){
    foreach($v as $pl){if($pl == $platform){$device = $k;break;}                        

return array( 'device' => $device, 'platform' => $platform, 'browser' => $browser, 'version' => $version );

Comment by: Jesse G. Donat on

Jesse G. Donat GravatarI'm sorry but I'm not interested in implementing anything along these lines right now. I think you would likely be better off simply creating a function that calls my function doing your desired analysis.

My goal is to keep this more of a strict parser and not an interpreter.

I really want to avoid feature creep.

Comment by: Russell G. on

Awesome! Made my day, thanks!

Comment by: Ueli on

Ueli GravatarThanks a lot for this great piece of code.
Just a very minor suggestion for the documentation. I wasn't sure if the currently detected items are effectively return values, or if they are just listed in a descriptive style. So I went into the code to be sure. This would be more obvious if the items were enclosed by quotes, for a list like
o Mobile
- 'Android'
- 'Windows Phone OS'
- 'Kindle Fire'

Comment by: Const on

  • Resolved
Const GravatarHi, what about the new Vivaldi browser?

The string is: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.89 Vivaldi/ Safari/537.36 and is identified as Chrome 40.

Comment by: Ben on

Ben GravatarHi - many thanks for this extremely useful tool. I was wondering if there was a way to further differentiate device types: at the moment, a distinction is made between "Desktop" and "Mobile" devices - could the latter be further split into "Phone" and "Tablet" devices? I know it's possible to tell iPhones and iPads apart, but is there any reliable way of doing similar for Androids? Can we tell Chrome on an Android tablet from Chrome on an Android phone?

Comment by: Cli on

Cli GravatarThis is pretty awesome as it is right now, but could you include Operating system versions as well? E.g. Windows 7 SP1, Windows 8, Windows Vista, Android 4, Android 5 etc?

Comment by: JOe on

JOe GravatarDoesn't work at all with PHP 5.6 !!!

Comment by: Jesse G. Donat on

Jesse G. Donat GravatarJOe, not true! It's actually extensively tested to work from PHP 5.3 all the way through PHP 7 - including HHVM.


If you had left an actual email address I'd have been happy to help you resolve your issues. Alas.

Comment by: Martin on

Martin GravatarThanks for this functionality, it's simple, with low footprint, and works.
Really appreciated. Keep up the good work.

Comment by: Philippe on

Philippe GravatarHi, great script, thank you very much.

Just one thing, it looks like Opera Neon is not detected correctly

Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.21 Safari/537.36 MMS/1.0.2531.0

Comment by: Jesse G. Donat on

Jesse G. Donat GravatarThey are making it hard to detect on purpose because they want to be detected as Chrome.

While I can update the code to sniff it out since it doesn't indicate what it actually is, it's not a mainstream enough browser to justify the additional code. Worldwide usage is basically nil.

It's in their own words a "Concept Browser". If and when it becomes anything more than a concept, I will gladly support it, but until then…

Comment by: Jose on

Jose GravatarNice work bro!!

Comment by: Gonzo Far on

Gonzo Far GravatarThank you for you work.
Why am I getting these errors?

Warning: Unexpected character in input: '\' (ASCII=92) state=1 in C:\dev\UserAgentParser.php on line 23

Parse error: syntax error, unexpected ':' in C:\dev\UserAgentParser.php on line 73

Comment by: Jesse G Donat on

Jesse G Donat GravatarSorry I didn't see this comment until now. It would appear you are running a very old PHP version - pre namespaces.

My library supports all the way back to PHP 5.3.0 which is 11 years old. You're running older something older than that.

It's probably time to upgrade.

Email address will never be publicly visible.

See my Tweet about comment formatting.