donatj / phpuseragentparser
Lightning fast, minimalist PHP UserAgent string parser.
Fund package maintenance!
www.paypal.me/donatj/15
Ko Fi
donatj
Installs: 14 904 481
Dependents: 56
Suggesters: 6
Security: 0
Stars: 565
Watchers: 34
Forks: 127
Open Issues: 7
Requires
- php: >=5.4.0
- ext-ctype: *
Requires (Dev)
- ext-json: *
- camspiers/json-pretty: ~1.0
- donatj/drop: *
- phpunit/phpunit: ~4.8|~9
- dev-master
- v1.10.0
- v1.9.0
- v1.8.0
- v1.7.0
- v1.6.1
- v1.6.0
- v1.5.0
- v1.4.0
- v1.3.0
- v1.2.0
- v1.1.0
- v1.0.0
- v0.23.0
- v0.22.0
- v0.21.1
- v0.21.0
- v0.20.0
- v0.19.0
- v0.18.0
- v0.17.0
- v0.16.0
- v0.15.0
- v0.14.0
- v0.13.0
- v0.12.0
- v0.11.1
- v0.10.0
- v0.9.0
- v0.8.0
- v0.7.0
- v0.6.0
- v0.5.3
- v0.5.2
- v0.5.1
- v0.5.0
- v0.4.0
- v0.3.6
- v0.3.5
- v0.3.4
- v0.3.3
- v0.3.2
- v0.3.1
- v0.3.0
- v0.2.2
- v0.2.1
- v0.2.0
- v0.1.6
- v0.1.5
- v0.1.4
- v0.1.3
- v0.1.2
- v0.1.1
- v0.1.0
- dev-donatj-patch-1
- dev-phpstan2
- dev-retro
- dev-81_nightly
- dev-reorg
- dev-patch-1
- dev-os_version_detection
This package is auto-updated.
Last update: 2024-11-24 12:13:39 UTC
README
What It Is
A simple, streamlined PHP user-agent parser!
Licensed under the MIT license: https://www.opensource.org/licenses/mit-license.php
Upgrading to 1.*
The new 1.*
release does not break compatibility with 0.*
and nothing need to change to upgrade. However, the global parse_user_agent
is now deprecated; it has been replaced with the namespaced \donatj\UserAgent\parse_user_agent
and functions exactly the same. You can easily replace any existing call to parse_user_agent
with \donatj\UserAgent\parse_user_agent
In addition, 1.x adds a convenience object wrapper you may use should you prefer. More information on this is in the Usage section below.
Why Use This
You have your choice in user-agent parsers. This one detects all modern browsers in a very light, quick, understandable fashion. It is less than 200 lines of code, and consists of just three regular expressions! It can also correctly identify exotic versions of IE others fail on.
It offers 100% unit test coverage, is installable via Composer, and is very easy to use.
What It Does Not Do
This is not meant as a browser "knowledge engine" but rather a simple parser. Anything not adequately provided directly by the user agent string itself will simply not be provided by this.
OS Versions
User-agent strings are not a reliable source of OS Version!
- Many agents simply don't send the information.
- Others provide varying levels of accuracy.
- Parsing Windows versions alone almost nearly doubles the size of the code.
I'm much more interested in keeping this thing tiny and accurate than adding niché features and would rather focus on things that can be done well.
All that said, there is the start of a branch to do it I created for a client if you want to poke it, I update it from time to time, but frankly if you need to reliably detect OS Version, using user-agent isn't the way to do it. I'd go with JavaScript.
Undetectable Browsers
- Brave - Brave is simply not differentiable from Chrome. This was a design decision on their part.
Undetectable Platforms
- iPadOS 13+ - Starting with iPadOS 13 and further hardened in 14, iPadOS returns the exact same string as macOS. It is no longer distinguishable by UA string. (See: #69)
Requirements
- php: >=5.4.0
- ext-ctype: *
Installing
PHP User Agent is available through Packagist via Composer.
Install the latest version with:
composer require 'donatj/phpuseragentparser'
Usage
The classic procedural use is as simple as:
<?php // if you're using composer require __DIR__ . '/../vendor/autoload.php'; // v0 style global function - @deprecated $uaInfo = parse_user_agent(); // or // modern namespaced function $uaInfo = donatj\UserAgent\parse_user_agent(); echo $uaInfo[donatj\UserAgent\PLATFORM] . PHP_EOL; echo $uaInfo[donatj\UserAgent\BROWSER] . PHP_EOL; echo $uaInfo[donatj\UserAgent\BROWSER_VERSION] . PHP_EOL;
The new object-oriented wrapper form:
<?php use donatj\UserAgent\UserAgentParser; // if you're using composer require __DIR__ . '/../vendor/autoload.php'; $parser = new UserAgentParser(); // object-oriented call $ua = $parser->parse(); // or // command style invocation $ua = $parser(); echo $ua->platform() . PHP_EOL; echo $ua->browser() . PHP_EOL; echo $ua->browserVersion() . PHP_EOL;
Currently Detected Platforms
Predefined helper constants from donatj\UserAgent\Platforms
Currently Detected Browsers
Predefined helper constants from donatj\UserAgent\Browsers
More information is available at Donat Studios.