Created
January 20, 2016 10:07
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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);