Introducing ua-parser-php: Slicing & Dicing User Agent Strings

ua-parser-php is a PHP-based pseudo-port of the ua-parser project. ua-parser-php is designed to parse a user agent string and return certain properties like browser name, OS version, and, in some cases, device name. ua-parser-php utilizes the user agents regex YAML file from ua-parser but otherwise uses its own set of properties to describe a browser, OS, and device. ua-parser-php was created as a new browser-detection library for the browser- and feature-detection library Detector.

If you want, you can test your browser against ua-parser-php. As usual, the code is available on GitHub.


Using ua-parser-php is straightforward. Simply include it in your project, make the call to parse the user agent and you’ll be returned an object with various properties of the user agent. Examples follow:


   $result = UA::parse();

   print $result->full;
   // -> Chrome 16.0.912/Mac OS X 10.6.8

   print $result->browserFull;
   // -> "Chrome 16.0.912"

   print $result->browser;
   // -> "Chrome"

   print $result->version;
   // -> "16.0.912"

   print $result->major;
   // -> 16 (minor, build, & revision also available)

   print $result->osFull;
   // -> "Mac OS X 10.6.8"

   print $result->os;
   // -> "Mac OS X"

   print $result->osVersion;
   // -> "10.6.8"

   print $result->osMajor;
   // -> 10 (osMinor, osBuild, & osRevision also available)

   print $result->uiwebview;
   // -> (boolean, would return true if the user agent was from a uiwebview in ios)

* in select cases the following will also work...

   print $result->deviceFull;
   // -> "Palm Pixi 1.0"
   print $result->device;
   // -> "Palm Pixi"

   print $result->deviceVersion
   // -> "1.0"

   print $result->deviceMajor;
   // -> 1 (deviceMinor also available)

   print $result->tablet;
   // (boolean, would return true the device was a tablet according to the user agent information)

view raw This Gist brought to you by GitHub.

If you want to grab a copy of the YAML data from ua-parser each night you can use a cron job and point it at the following bit of code:


   $result = UA::get();

view raw This Gist brought to you by GitHub.

NOTE: Using this feature will overwrite a number of changes I’ve made to the user_agents_regex.yaml file included with the ua-parser-php distribution.


Thanks to the ua-parser team for making the core data available to others so we can build our own solutions.

About This Article
David Olsen