Posted By


xuanyan on 05/12/08

Tagged


Statistics


Viewed 526 times
Favorited by 0 user(s)

get_os_browswer_by_ua


/ Published in: Other
Save to your folder(s)



Copy this code and paste it in your HTML
  1. function get_os_browser($ua)
  2. {
  3. $low_ua = strtolower($ua);
  4. if (strpos($low_ua,'maxthon') !== false)
  5. {
  6. preg_match('/maxthon[^\)|\(|\;]*/i',$ua,$match);
  7. }
  8. elseif (strpos($low_ua,'tencenttraveler') !== false)
  9. {
  10. preg_match('/tencenttraveler[^\)|\(|\;]*/i',$ua,$match);
  11. }
  12. elseif (strpos($low_ua,'msie') !== false)
  13. {
  14. preg_match('/msie[^\)|\(|\;]*/i',$ua,$match);
  15. }
  16. elseif (strpos($low_ua,'camino') !== false)
  17. {
  18. preg_match('/camino[^\)|\(|\;]*/i',$ua,$match);
  19. }
  20. elseif (strpos($low_ua,'opera') !== false)
  21. {
  22. preg_match('/opera[^\)|\(|\;]*/i',$ua,$match);
  23. }
  24. elseif (strpos($low_ua,'navigator') !== false)
  25. {
  26. preg_match('/navigator[^\)|\(|\;]*/i',$ua,$match);
  27. }
  28. elseif (strpos($low_ua,'firefox') !== false)
  29. {
  30. preg_match('/firefox[^\)|\(|\;]*/i',$ua,$match);
  31. }
  32. elseif (strpos($low_ua,'safari') !== false)
  33. {
  34. preg_match('/safari[^\)|\(|\;]*/i',$ua,$match);
  35. }
  36. else
  37. {
  38. $match[] = substr($ua,strrpos($ua,' '));
  39. }
  40. $browser = trim($match[0]);
  41.  
  42. if (strpos($low_ua,'windows') !== false)
  43. {
  44. preg_match('/([^;]*windows[^;]+);/i',$ua,$match);
  45. $rep = array(
  46. 'Windows NT 5.0' => 'Windows 2000',
  47. 'Windows NT 5.1' => 'Windows XP',
  48. 'Windows NT 5.2' => 'Windows 2003',
  49. 'Windows NT 6.0' => 'Windows Vista',
  50. );
  51. $match[1] = strtr($match[1], $rep);
  52. }
  53. elseif (strpos($low_ua,'iphone') !== false)
  54. {
  55. preg_match('/([^;(]*iphone[^;]*);/i',$ua,$match);
  56. }
  57. elseif (strpos($low_ua,'mac os') !== false)
  58. {
  59. preg_match('/([^;]*macs[^;]+);/i',$ua,$match);
  60. }
  61. else
  62. {
  63. $agent = substr($ua,strpos($ua,'(')+1,strpos($ua,')')-strpos($ua,'(')-1);
  64. $info = explode(';', $agent);
  65. $match[1] = isset($info[2]) ? trim($info[2]) : 'unknow';
  66. }
  67. $os = trim($match[1]);
  68.  
  69. return array($os,$browser);
  70. }
  71.  

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.