36

As strange as I find this, I have not been able to find a good PHP function anywhere which will do an intelligent parse of a user agent string? Googled it for about 20 minutes now.

I have the string already, I just need something that will chop it up and give me at least browser/ver/os.

Know of a good snippet anywhere?

3
  • Yes, I know I can write my own, but I really don't have time at the moment. I need something I can plug in and use to finish up this project. Commented Jan 23, 2010 at 10:12
  • get_browser() is quiet old and slow. Use one of the parser here github.com/ThaDafinser/UserAgentParser Commented Feb 15, 2016 at 9:26
  • warning against using the suggested library above (TheDafinser). It seems to work by pushing a request to another site (useragentstring.com) which as we speak is DOWN!!!! Since I was using it somewhere, it slowed my site like nobody's business since the request had to run until timeout. Be warned Commented Jan 31, 2017 at 15:51

5 Answers 5

23

The get_browser() function has been available in PHP for quite a long a time.

The PHP manual is free, can be downloaded in various formats and viewed online (with comments)

Sign up to request clarification or add additional context in comments.

9 Comments

This would be a perfect example of how being up for more than two days can affect memory and cause you to ask extremely stupid questions about functions you already know about. :) Thanks
The post below (github.com/garetjax/phpbrowscap) is what the PHP manual suggests.
Is there any performance penalty when enabling get_browser() function in php ?
@Bhavesh: that's a very complex question. Fortunately it has a very simple answer: measure it :)
Forget to mention that in order to use that function you need a ~10MB php_browscap.ini file. - Otherwise this error will occur: browscap ini directive not set
|
13

https://github.com/browscap/browscap-php - this is a standalone library that aims to replace get_browser function. For shared hostings it seems to be a better option.

Works on: PHP 5

Comments

3

You can try to use: https://github.com/tobie/ua-parser. This is multi-language tool to parsing user agent string. For PHP is here: https://github.com/tobie/ua-parser/tree/master/php

Comments

0

Try out ThaDafinser/UserAgentParser it's an abstraction for many available user agent parsers.

So if you are not happy with one - just switch to another (or combine them)

You can try it out here

2 Comments

gave me very many issues when the website they throw the processing requests to was down. It slowed my site badly.
Then you might used a remote parser like "useragentstring.com" (which is down lately). Of course that will then slow your website down. You can use a local parser when you rely on speed. See github.com/ThaDafinser/UserAgentParser#local-providers
0

There is a PHP class library i found out, and it has worked so well for me. It is in the link below.

https://www.toms-world.org/blog/parseuseragentstring

It is lightweight and just does what I wanted (Browser, Version, OS, Mobile/PC, and much more) in a memory-friendy manner.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.