Tablet and mobile device detection in PHP

Previously on mobiForge Ronan posted about a lightweight approach to device detection using regular expressions in PHP. Since this is still an approach widely adopted by many web developers today, we feel that it’s about time we revisited the original article.

For those new to the subject, the approach works by matching User-Agent strings of mobile devices using regular expressions. As we mentioned in the original article, using a PHP regex solution to detect devices can be a good solution for certain use cases. For example, for smaller websites without a high volume of traffic, or where the site owner is not too concerned about detecting their mobile traffic with very high accuracy, or where the goal is simple mobile or tablet redirection.

In this article, we provide an updated version of the original algorithm which includes explicit tablet detection, for those who want to optimise for their tablet visitors, and we also broaden the original regex patterns to catch some of the more recent mobile devices not detected by the original.

But before we jump in, there are limitations that you should be aware of before choosing this option.

  • Accuracy – Regex User-Agent matching can work well in the general case, but will inevitably fail to recognise some portion of devices correctly, e.g. certain Android tablets. And while it is might be possible to add more specific patterns to the regex to match edge-cases by matching specific model numbers, this has the disadvantage of reducing the perfomance of the detection, and will have knock-on performance implications for your site
  • Performance – Regular expressions can be slow to execute, particularly for complicated patterns. If performance is a key factor for you, then this is not the way to go
  • Maintenance – The regex patterns will need to be updated regularly to keep up to date with new devices that may not be already covered
  • Device capabilities – If you need anything more than simple traffic routing (mobile/tablet/desktop), for instance if you need to know device properties such as screen size, memory limit, HTML5 support, and so on, then this is not the solution for you

So, with those caveats out of the way, time for the code:

As in our previous article, the code is based on work by Andy Moore. The approach taken for tablet detection is a broad, (mostly) generic matching pattern. It should perform reasonably well, and will be suitable for you if accurate detection of every single tablet device is not critical. It will likely fail on a certain portion of tablets that are difficult to distinguish from their mobile counterparts without specifying individual model numbers.

We also added some special treatment for the proxy-based browser Opera Mini, which has recently started to send the new HTTP_DEVICE_STOCK_UA header that helps to identify the underlying device.

It should also perform reasonably well at detecting mobile browsers, but once again, if you require high accuracy, performance, or device properties, then you should look to a more dedicated device detection solution.

Exclusive tips, how-tos, news and comment

Receive monthly updates on the world of mobile dev.

Other Products

Try the world’s leading device detection solution at
DeviceAtlas - Try the world’s leading mobile device detection solution

Evaluate your websites’ mobile readiness with
mobiReady - Evaluate your websites’ mobile readiness.

© 2022 DeviceAtlas Limited. All rights reserved.

This is a website of DeviceAtlas Limited, a private company limited by shares, incorporated and registered in the Republic of Ireland with registered number 398040 and registered office at 6th Floor, 2 Grand Canal Square, Dublin 2, Ireland