• Stars
    star
    564
  • Rank 78,739 (Top 2 %)
  • Language
    PHP
  • License
    MIT License
  • Created over 13 years ago
  • Updated about 1 month ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Lightning Fast, Minimalist PHP User Agent String Parser.

PHP User Agent Parser

Join the chat at https://gitter.im/PhpUserAgentParser/Lobby

Latest Stable Version Total Downloads License ci.yml

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

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

// 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;

$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

Constant Platform
Platforms::MACINTOSH Macintosh
Platforms::CHROME_OS Chrome OS
Platforms::LINUX Linux
Platforms::WINDOWS Windows
Platforms::ANDROID Android
Platforms::BLACKBERRY BlackBerry
Platforms::FREEBSD FreeBSD
Platforms::IPAD iPad
Platforms::IPHONE iPhone
Platforms::IPOD iPod
Platforms::KINDLE Kindle
Platforms::KINDLE_FIRE Kindle Fire
Platforms::NETBSD NetBSD
Platforms::NEW_NINTENDO_3DS New Nintendo 3DS
Platforms::NINTENDO_3DS Nintendo 3DS
Platforms::NINTENDO_DS Nintendo DS
Platforms::NINTENDO_SWITCH Nintendo Switch
Platforms::NINTENDO_WII Nintendo Wii
Platforms::NINTENDO_WIIU Nintendo WiiU
Platforms::OPENBSD OpenBSD
Platforms::PLAYBOOK PlayBook
Platforms::PLAYSTATION_3 PlayStation 3
Platforms::PLAYSTATION_4 PlayStation 4
Platforms::PLAYSTATION_5 PlayStation 5
Platforms::PLAYSTATION_VITA PlayStation Vita
Platforms::SAILFISH Sailfish
Platforms::SYMBIAN Symbian
Platforms::TIZEN Tizen
Platforms::WINDOWS_PHONE Windows Phone
Platforms::XBOX Xbox
Platforms::XBOX_ONE Xbox One

Currently Detected Browsers

Predefined helper constants from donatj\UserAgent\Browsers

Constant Browser
Browsers::ADSBOT_GOOGLE AdsBot-Google
Browsers::ANDROID_BROWSER Android Browser
Browsers::APPLEBOT Applebot
Browsers::BAIDUSPIDER Baiduspider
Browsers::BINGBOT bingbot
Browsers::BLACKBERRY_BROWSER BlackBerry Browser
Browsers::BROWSER Browser
Browsers::BUNJALLOO Bunjalloo
Browsers::CAMINO Camino
Browsers::CHROME Chrome
Browsers::CURL curl
Browsers::EDGE Edge
Browsers::FACEBOOKEXTERNALHIT facebookexternalhit
Browsers::FEEDVALIDATOR FeedValidator
Browsers::FIREFOX Firefox
Browsers::GOOGLEBOT Googlebot
Browsers::GOOGLEBOT_IMAGE Googlebot-Image
Browsers::GOOGLEBOT_VIDEO Googlebot-Video
Browsers::HEADLESSCHROME HeadlessChrome
Browsers::IEMOBILE IEMobile
Browsers::IMESSAGEBOT iMessageBot
Browsers::KINDLE Kindle
Browsers::LYNX Lynx
Browsers::MIDORI Midori
Browsers::MIUIBROWSER MiuiBrowser
Browsers::MSIE MSIE
Browsers::MSNBOT_MEDIA msnbot-media
Browsers::NETFRONT NetFront
Browsers::NINTENDOBROWSER NintendoBrowser
Browsers::OCULUSBROWSER OculusBrowser
Browsers::OPERA Opera
Browsers::PUFFIN Puffin
Browsers::SAFARI Safari
Browsers::SAILFISHBROWSER SailfishBrowser
Browsers::SAMSUNGBROWSER SamsungBrowser
Browsers::SILK Silk
Browsers::TELEGRAMBOT TelegramBot
Browsers::TIZENBROWSER TizenBrowser
Browsers::TWITTERBOT Twitterbot
Browsers::UC_BROWSER UC Browser
Browsers::VALVE_STEAM_TENFOOT Valve Steam Tenfoot
Browsers::VIVALDI Vivaldi
Browsers::WGET Wget
Browsers::WORDPRESS WordPress
Browsers::YANDEX Yandex
Browsers::YANDEXBOT YandexBot

More information is available at Donat Studios.

More Repositories

1

CsvToMarkdownTable

Simple JavaScript/Node.js CSV to Markdown Table Converter
JavaScript
291
star
2

mock-webserver

Simple mock web server in PHP for unit testing.
PHP
131
star
3

RewriteRule-Generator

Tool to generate .htaccess RewriteRule's from a tabbed set of data
PHP
128
star
4

Circle-Generator

Tool to generate circles for block building games like Minecraft
TypeScript
109
star
5

SimpleCalendar

A simple PHP calendar rendering class
PHP
81
star
6

CLI-Toolkit

PHP CLI Toolkit
PHP
60
star
7

sqlread

SQL Dump Parser - Query MySQL Dumps Directly without loading them into MySQL
Go
48
star
8

hookah

GitHub Webhooks Made Easy!
Go
32
star
9

mjpeg-php

Toy long-polling motion jpeg example script.
PHP
31
star
10

imgdedup

CLI tool for image duplicate detection
Go
31
star
11

imgboard

Super Fun JavaScript-Free Drawing Board
Go
29
star
12

Flags

A GNU-style PHP command line argument parser inspired by Go's Flag package.
PHP
19
star
13

mpo

JPEG-MPO Decoder / Converter Library and CLI Tool
Go
17
star
14

gojs

Run JavaScript in Go in WASM in the Browser
JavaScript
13
star
15

XLS-XML-Formula-Expander

Parses through SpreadsheetML (Excel 2003 XML) Spreadsheets, and evaluates their formulas.
PHP
10
star
16

force-color.org

Website data for force-color.org
SCSS
10
star
17

BeautifulPHPCLI-talk

Beautiful PHP CLI Talk Code Samples
PHP
9
star
18

Pushover-PHP

Simple Pushover API Wrapper for PHP
PHP
9
star
19

wordwrap

UTF-8 Unicode Safe Golang Byte Size WordWrap
Go
9
star
20

gifopt

Simple Interframe Gif Optimizer
Go
8
star
21

pocketcasts-go

Go connector for Pocket Casts Private API
Go
8
star
22

Boomerang

API Endpoint E2E Testing Application
PHP
7
star
23

tabasco

Chrome Extension to Wrangle Tabs
JavaScript
6
star
24

PHP-JPGMPO-to-Stereo-JPEG

Converts Nintendo 3DS MPO files to side by side JPEG Images
PHP
5
star
25

PHP-Minecraft-Bukkit-Server-Status

PHP
5
star
26

CorpusPHP

Corpus PHP Framework
PHP
5
star
27

PhpIniBuilder

PHP `parse_ini_string` Compatible INI Builder
PHP
4
star
28

printf-parser

A PHP compatible tokenizing printf string parser
PHP
4
star
29

Hidden-File-Toggle-Widget

An OS X widget to toggle display of hidden files.
JavaScript
4
star
30

unic

Like UNIX `sort | uniq` except it's quicker and maintains order. Uses a Cuckoo Filter.
Go
4
star
31

SlenderGrid

Painfully Simple CSS Grid Using Sass
CSS
4
star
32

jqmux

Go HTTP Multiplexer routing on JSON http request bodies via jq queries
Go
3
star
33

tcgif

True Color Gif Generator
Go
3
star
34

mddom

A Basic Markdown "DOM" for building Markdown out of Objects
PHP
3
star
35

hmacsig

HMAC Signature Validation Middleware (e.g. for GitHub Webhooks)
Go
3
star
36

DatePicker

Simple No-Framework No-Nonsense JavaScript Date Picker
JavaScript
3
star
37

Pinch

Super Simple Event Driven PHP IRC Bot
PHP
3
star
38

masq

Masq ain't SQL. A reimplementation of Misstep in Go using a more traditional parse tree
Go
3
star
39

gifdump

Dump the frames of a gif, composed or uncomposed
Go
3
star
40

appsettings

Simple Hierarchical Key/Value Store for Simple Go Runtime App Setting Storage
Go
3
star
41

TickerGraph

Simple JavaScript Real Time Ticker Graph
TypeScript
3
star
42

AlikeColorFinder

Finds Alike Colors in CSS, SCSS, LESS etc.
PHP
3
star
43

pngpal

CLI tool to convert PNGs to palleted PNGs for amazing space savings.
Go
2
star
44

DB-Analyze

PHP Database Analyzer
PHP
2
star
45

JDLightbox

Super Simple No Frills No Framework Lightbox
JavaScript
2
star
46

parsestr

Provides PHP "parse_str" style URL parsing to Golang
Go
2
star
47

ghemoji

Golang tools for working with old style github :emoji:
Go
2
star
48

symlink-check-action

Action to validate all symlinks in a project are valid
Shell
1
star
49

3D-Package-JS

Fun Little JavaScript 3D Engine
HTML
1
star
50

FolderFileOlderThan

Simple VB.Net scanner to scan a folders folders for files max modified dates
Visual Basic
1
star
51

Clipboard-2-Desktop

Tiny windows app, saves clipboard contents to the desktop
Visual Basic
1
star
52

hqxgo

HQX Go
Go
1
star
53

3D-Package

PHP
1
star
54

php-dnf-solver

PHP DNF (Disjunctive Normal Form) Signature Compatibility Solver
PHP
1
star
55

Spacecat

Exciting and New
1
star
56

diskWatcher

Watches disk space, notifies with pushover on problems
PHP
1
star
57

Webarchive-PHP

Webarchive-PHP is a small library for exporting Safari "Webarchive" files.
PHP
1
star
58

Advanced-File-Filter

An Advanced .Net File Filtering Library
Visual Basic
1
star
59

Amazon-FWS

Simple Amazon Fulfillment Web Service Connector
PHP
1
star
60

depr

Tool for archiving old "depr"ecated files/projects
Go
1
star
61

CanvasFuntime

1
star
62

jdzoom

A JavaScript Zoom application
JavaScript
1
star
63

imgavg

A simple go tool to average a collection of images faster than Photoshop.
Go
1
star
64

explainer

Simple naive mysql database profiler
Go
1
star
65

File-Exist-O-Tron

Determines if a list of files exist in a directory or below.
Visual Basic
1
star
66

prefwatch

Watch system perference plists for changes and report
Go
1
star
67

mddoc

A simple, directed, markdown documentation generator for PHP
PHP
1
star