Skip to content

Instantly share code, notes, and snippets.

@jamiehollern
Created January 20, 2016 10:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamiehollern/3b8eb88e94fb7bf1f923 to your computer and use it in GitHub Desktop.
Save jamiehollern/3b8eb88e94fb7bf1f923 to your computer and use it in GitHub Desktop.
Regular expression to get the iOS version from the user agent string in PHP
<?php
// Sample user agent strings available at http://www.webapps-online.com/online-tools/user-agent-strings/dv/operatingsystem51849/ios.
$user_agent = 'Mozilla/5.0 (iPod touch; CPU iPhone OS 7_0_3 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11B511 Safari/9537.53';
// Returns 7.0 with the above user agent string.
$version = preg_replace("/(.+)(iPhone|iPad|iPod)(.+)OS[\s|\_](\d)\_?(\d)?[\_]?(\d)?.+/i", "$4.$5", $user_agent);
@grisha2217
Copy link

grisha2217 commented Dec 30, 2022

Better version
Return 16.2

$user_agent = 'Mozilla/5.0 (iPhone; CPU iPhone OS 16_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) CriOS/108.0.5359.112 Mobile/15E148 Safari/604.1'; $version = preg_replace("/(.+)(iPhone|iPad|iPod)(.+)OS[\s|\_](\d+)\_?(\d+)?[\_]?(\d+)?.+/i", "$4.$5", $user_agent);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment