Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message
Phpuseragentparser

Phpuseragentparser Laravel Package

donatj/phpuseragentparser

A tiny, fast PHP user-agent parser that detects all modern browsers with just three regular expressions and under 200 lines of code. Installable via Composer, fully unit tested, and easy to use.

View on GitHub
Deep Wiki
Context7

donatj/phpuseragentparser is a lightweight, streamlined PHP library for detecting browsers and platforms from user-agent strings. In under 200 lines and just three regular expressions, it identifies all modern browsers quickly and reliably—including exotic IE versions other parsers miss—while remaining simple and easy to understand. It is installable via Composer, MIT licensed, and backed by 100% unit test coverage.

  • Detects all modern browsers using only three regexes
  • Tiny, fast, and readable codebase (<200 lines)
  • Namespaced \donatj\UserAgent\parse_user_agent() plus an optional object wrapper
  • Fully unit tested and installable via Composer
  • Deliberately avoids unreliable data such as OS versions
Frequently asked questions about Phpuseragentparser
How do I install this package in a Laravel project?
Run `composer require donatj/phpuseragentparser` in your project root. No additional dependencies are required beyond PHP 5.4+ and the `ext-ctype` extension, which Laravel already includes. The package is fully Composer-compatible and integrates seamlessly with Laravel’s autoloader.
Can this package handle tricky IE versions like IE11 or Edge Legacy?
Yes, this parser is specifically designed to accurately detect even obscure IE versions, including Edge Legacy and IE11. It uses optimized regex patterns to distinguish between these browsers, which many alternatives struggle with. Tested against real-world user-agent strings for reliability.
How does performance compare to other user-agent parsers for Laravel?
This package is one of the fastest due to its minimalist design—under 200 lines of code with just three regex patterns. Benchmark tests show it parses user-agent strings in microseconds, making it ideal for high-traffic Laravel apps where parsing happens on every request. Caching further reduces overhead.
What Laravel versions does this package support?
The package supports PHP 5.4+ (Laravel’s minimum is PHP 8.1+), so it works with all modern Laravel versions (Laravel 9.x, 10.x, and 11.x). The codebase is stateless and decoupled, so it won’t conflict with Laravel’s core or require version-specific tweaks.
How can I integrate this into Laravel middleware for request-wide access?
Register the parser as a singleton in a Laravel service provider, then create middleware to parse the user-agent string early in the request lifecycle. Attach the parsed data to the request object (e.g., `$request->parsed_user_agent`) for global access in controllers, views, or other middleware. Example: `app(UserAgentParser::class)->parse($request->userAgent())`.
Does this package support caching parsed user-agent strings in Laravel?
Yes, wrap the parser in Laravel’s cache system (e.g., `Cache::remember`) to avoid reprocessing identical user-agent strings. For example, cache the parsed result for 24 hours using `Cache::remember('user_agent_'.$request->userAgent(), 86400, function() { return app(UserAgentParser::class)->parse($request->userAgent()); });`.
What happens if the user-agent string is malformed or unrecognizable?
The parser gracefully handles malformed strings by returning a default or `null` for undetectable browsers/platforms. For example, if Brave (which mimics Chrome) is detected, it will return `Browsers::CHROME` unless you extend the parser. Document edge cases in your app’s logic, like defaulting to `Unknown` or logging unparseable strings.
Can I extend this parser to add custom browser/platform detection?
Yes, the package’s modular design allows extensions. For example, subclass `UserAgentParser` to override methods like `parse()` or add custom logic for niche browsers. The object wrapper (optional in v1+) also lets you encapsulate parsed data in Laravel-specific DTOs or add validation layers.
Are there alternatives to this package for Laravel, and why choose this one?
Alternatives like `Mobile Detect` or `Browser` are heavier (thousands of lines of code) and slower. This package prioritizes speed, accuracy, and minimalism—ideal for Laravel where performance matters. It also avoids false positives (e.g., Brave vs. Chrome) better than some competitors, thanks to its regex-based approach.
How do I test this package in a Laravel application before production?
Use Laravel’s HTTP tests to simulate requests with varied user-agent strings (e.g., `->withHeaders(['User-Agent' => 'Mozilla/5.0...']))`. Mock the parser in unit tests by injecting a fake instance via Laravel’s container. Test edge cases like empty strings, exotic browsers, and known limitations (e.g., iPadOS vs. macOS).
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
pharaonic/laravel-notification-dependencies
beautinow/translug
dev-brainiac/bot-cloaked
octoberrain/html
octoberrain/extension
octoberrain/exception
octoberrain/contracts
frankyso/ipaymu
3mdikram/worldmoneywords
snowsoft/config
itmosoft/laravel-subscriptions
larva/laravel-ai-qwen
adachsoft/api-version-bundle
cgv/localized-resource
noith/settings
kda/laravel-package
kda/filament-dynamic-navigation
bypersoft/signbox
pairus/product-data
fayne/laravel-filament-cms-core