Description
parseUserAgent is a function that takes a user agent and returns the browser name and version (and soon the operating system and version).
The Wheel
There are many libraries and solutions for this problem, but I decided to re-invent the wheel and write my own as I've not found one that did all of the following:
- Pattern-based, I don't want to store huge lists of known-good agents
- Local, I don't want to send requests to (or pay for) an external API
- Reliable, I've tested lots of libraries, many have incorrect results
- Specific, I don't just want the main 6~ and then "Other"
Todo
- Operating System & Version parsing
- Explore cache (normal, session, or temp-file) solutions
PHP
Usage
<?php
$user_agent = $_SERVER['HTTP_USER_AGENT'];
print_r(
parseUserAgent($user_agent)
);
The above code will output:
Array
(
[browser_name] => Firefox
[browser_version] => 4.0b9pre
)
JavaScript
let user_agent = request.headers["user-agent"];
console.table(
parseUserAgent(user_agent)
);
The above code will output:
┌─────────────────┬────────────┐
│ (index) │ Values │
├─────────────────┼────────────┤
│ browser_name │ 'Firefox' │
│ browser_version │ '4.0b9pre' │
└─────────────────┴────────────┘