Skip to content

Instantly share code, notes, and snippets.

@Fonger
Created July 24, 2014 15:39
Show Gist options
  • Save Fonger/6ac0ba31e19115f6fbc3 to your computer and use it in GitHub Desktop.
Save Fonger/6ac0ba31e19115f6fbc3 to your computer and use it in GitHub Desktop.
<?php
$browsers = array('firefox','chrome','safari','opera','msie');
$browser = new stdClass();
preg_match('#('.implode('|',$browsers).')(/| )([^ ]+)#i',$_SERVER['HTTP_USER_AGENT'],$match);
$browser->version = round((int)$match[3]);
$match = strtolower($match[1]);
foreach($browsers as $val)
{
$browser->{$val} = ($match == $val ? TRUE : FALSE);
}
if($browser->safari || $browser->opera)
{
preg_match('#version/([^ ]+)#i',$_SERVER['HTTP_USER_AGENT'],$match);
$browser->version = round((int)$match[1]);
}
$browser->webkit = ($browser->chrome || $browser->safari ? TRUE : FALSE);
var_dump($browser);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment