Parsing the User Agent String in PHP

JParra April 8'th 2008 03:00:45 pm

Recently I've been working on the stats logger of the blog and stumbled upon a typical WWW problem. The lack of standars, or more accurately the lack of browsers following standards, this time with the User Agent String.

I googled a bit and couldn't find the thing I was going after, the closest thing I found was this function on dotvoid.com, which is pretty limited to the goal I'm after.

So I decided to create a new user agent parser from the ground up, the  goal was to extract as much information as possible as simply as possible, the resulting code consist of 2 functions:


  1. function parse_useragent($agent){
  2. $parts = preg_split('/(?(?![^\(]*\))\s(?!\()|x{7})/', $agent);
  3. $data = array();
  4. foreach($parts as $key => $value){
  5. switch($key){
  6. case 0:
  7. list($data['product'], $comment) = explode(' (', $value);
  8.  
  9. $info = analyze_product_comment($comment);
  10. break;
  11. case 1:
  12. $data['engine'] = $value;
  13. break;
  14. case 2:
  15. if(stristr($value, 'version'))
  16. $data['engine'] .= " $value";
  17. else
  18. $data['browser'] = $value;
  19. break;
  20. case 3:
  21. $data['browser'] = $value;
  22. break;
  23. case 4:
  24. $data['browser'] = $value . " " .$parts[3];
  25. break;
  26. }
  27. }
  28. return $data+$info;
  29. }
  30. function analyze_product_comment($value){
  31. $value = str_ireplace(array("(", ")"), "", $value);
  32. $parts = explode("; ", $value);
  33. foreach($parts as $key => $val){
  34. switch($key){
  35. case 0:
  36. if(strtolower($val) == 'compatible'){//Usually IE or bots
  37. $info = array(
  38. 'browser' => $parts[1],//Browser
  39. 'os' => $parts[2]//OS
  40. );
  41. return $info;
  42. }
  43. $info['platform'] = $val;//Platform
  44. break;
  45. case 1:
  46. if(eregi('[uin]', $val))
  47. $info['security'] = $val;//Security
  48. break;
  49. case 2:
  50. $info['os'] = $val;//OS
  51. break;
  52.  
  53. case 3:
  54. if(eregi('[a-z\-]{2,6}', $val))
  55. $info['language'] = $val;//Language
  56. break;
  57. }
  58. }
  59. return $info;
  60. }

And you just use it by calling the parse_useragent function with the user agent as the first parameter.

Example:

  1. $client = parse_useragent($_SERVER['HTTP_USER_AGENT']);
  2. print_r($client);
  3. /* Outputs
  4. array (
  5.   'product' => 'Mozilla/5.0',
  6.   'engine' => 'Gecko/2008032619',
  7.   'browser' => 'Firefox/3.0b5',
  8.   'platform' => 'Macintosh',
  9.   'security' => 'U',
  10.   'os' => 'Intel Mac OS X 10.5',
  11.   'language' => 'en-US',
  12. )
  13. */

Just a friendly reminder. All the code I post in here is licensed under an MIT License unless explicitly stated.

4 Comments Permalink Tags: php, stats, snippet