Spot
Spot

Reputation: 8155

Code to parse user agent string?

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?

Upvotes: 36

Views: 45027

Answers (5)

gthuo
gthuo

Reputation: 2566

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.

Upvotes: 0

ThaDafinser
ThaDafinser

Reputation: 509

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

Upvotes: 0

MartyIX
MartyIX

Reputation: 28676

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

Upvotes: 13

Piotr Olaszewski
Piotr Olaszewski

Reputation: 6214

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

Upvotes: 3

symcbean
symcbean

Reputation: 48387

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)

Upvotes: 24

Related Questions