How to get exact browser name and version?

How to get exact browser name and version?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Parsing User Agent Strings: Beyond Simple Regex

Relying on $_SERVER['HTTP_USER_AGENT'] to identify a browser is a common starting point, but as you've discovered, it quickly becomes a trap for naive parsing. The string provided by the client is not a structured object; it is a concatenation of tokens that often include legacy identifiers. In your specific example, the string contains Mozilla/5.0, which dates back to Netscape Navigator and is now a generic compatibility token required by many browsers. It also includes Safari/534.24 because Chrome historically needed to appear as a Safari-based engine to maintain web compatibility with sites that only supported WebKit at the time.

If you simply scan this string for "Chrome" or "Firefox," you risk false positives or incorrect version detection. The core problem is that the User-Agent string is not self-documenting in a way that supports simple string splitting. It requires a parsing strategy that understands the hierarchy of these tokens: the rendering engine, the operating system, and finally, the specific browser product.

A robust approach involves using a dedicated library rather than writing your own regular expression or splitting logic. In PHP, the browscap library is a standard solution that maps known patterns to specific data. However, the modern industry standard has largely shifted away from static pattern matching libraries toward machine-learning-based parsers because browser vendors frequently change their string formats without warning. Libraries like pecl/http-user-agent or symfony/browser-kit utilize these more advanced models to parse the string into a structured array containing properties like browser_name, browser_version, and os_name.

Here is how you might approach this in PHP using a library that handles the complexity:

use Symfony\Component\BrowserKit\AbstractBrowser;
use Symfony\Component\HttpKernel\EventListener\UserAgentParser;

// Note: Direct instantiation of UserAgentParser requires a full Symfony setup.
// For lightweight projects, packages like 'erwinos/useragent' are often preferred.
$parser = new \UserAgentParser(); // Example using a popular lightweight package
$userAgent = $_SERVER['HTTP_USER_AGENT'];

$result = $parser->parse($userAgent);

echo "Browser: " . ($result->browser ?? 'Unknown') . "\n";
echo "Version: " . ($result->version ?? 'Unknown') . "\n";

The output of such a parser will cleanly separate the noise. It will tell you the browser is Chrome and the version is 11.0.696.68, ignoring the Mozilla and Safari tokens that clutter the raw string. This separation is critical because relying on the raw string makes your application brittle. If Google decides to rename their engine or change the order of tokens in a future release, a regex-based parser breaks immediately. A library that updates its model regularly adapts to these changes automatically.

There is also a significant trade-off between accuracy and performance. While parsing the User-Agent string adds a negligible amount of CPU overhead on modern servers, it does require an initial setup (downloading rule sets for libraries like browscap) or network latency if using cloud-based APIs. For high-traffic applications, static regex patterns are sometimes used to quickly filter out desktop browsers before falling back to a library for complex cases, but this adds complexity without guaranteeing better results.

Furthermore, you must consider the context of your application. If you are running a simple script, installing a package via Composer is usually sufficient. If you are building a massive platform where every millisecond counts, you might evaluate whether identifying the specific browser version is actually necessary for your use case. Sometimes knowing just "Desktop vs Mobile" is enough to route traffic correctly, and deep diving into "Chrome 11.0 on Ubuntu 10.04" versus "Chrome 11.0 on Windows 7" may not provide actionable data unless you are specifically debugging rendering issues or supporting legacy enterprise software.

Ultimately, the raw string is a historical artifact, not a data format. Treating it as such leads to fragile code. By delegating the parsing logic to a specialized library, you ensure that your application identifies Google Chrome 11.0.696.68 accurately, regardless of how many other browser names are embedded in that single line of text. This approach saves you from maintaining complex regular expressions and protects your codebase from breaking when browser vendors change their reporting formats.

Our network