Im using get_browser and I only want to echo parts of what is returned.

So say for example I just want to echo platform and browser from what the array returns. How would I do that.

Thanks :)

Recommended Answers

All 3 Replies

get_browser returns an array if the second paramenter is true, of the remote user if the first parameter is null
print part of the array

<?php
$browser = get_browser(null, true);
print_r($browser['platform']);
print_r($browser['version']);
// its been a while echo $browser['platform']; may work
?>

array elements I know about, havent looked up if this is the full list
browser_name_regex
browser_name_pattern
parent
platform
browser
version
majorver
minorver
css
frames
tables
cookies
backgroundsounds
vbscript
javascript
javaapplets
activexcontrols
win16
wap

// browser detection
$is_lynx = $is_gecko = $is_winIE = $is_macIE = $is_opera = $is_NS4 = $is_safari = $is_chrome = false;
 
if (strpos($_SERVER['HTTP_USER_AGENT'], 'Lynx') !== false) {
	$is_lynx = true;
      $a="lynx";
} elseif ( strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'chrome') !== false ) {
	$is_chrome = true;
      $a="chrome";
} elseif ( strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'webkit') !== false ) {
	$is_safari = true;
      $a="safari";
} elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'Gecko') !== false) {
	$is_gecko = true;
      $a="firefox";
} elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false && strpos($_SERVER['HTTP_USER_AGENT'], 'Win') !== false) {
	$is_winIE = true;
      $a="win_IE";
} elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false && strpos($_SERVER['HTTP_USER_AGENT'], 'Mac') !== false) {
	$is_macIE = true;
      $a="mac_IE";
} elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'Opera') !== false) {
	$is_opera = true;
      $a="opera";
} elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'Nav') !== false && strpos($_SERVER['HTTP_USER_AGENT'], 'Mozilla/4.') !== false) {
	$is_NS4 = true;
      $a="netscape";
}
 
$is_IE = ( $is_macIE || $is_winIE );

$OSList = array
(

        // Match user agent string with operating systems

        'Windows 3.11' => 'Win16',

        'Windows 95' => '(Windows 95)|(Win95)|(Windows_95)',

        'Windows 98' => '(Windows 98)|(Win98)',

        'Windows 2000' => '(Windows NT 5.0)|(Windows 2000)',

        'Windows XP' => '(Windows NT 5.1)|(Windows XP)',

        'Windows Server 2003' => '(Windows NT 5.2)',

        'Windows Vista' => '(Windows NT 6.0)',

        'Windows 7' => '(Windows NT 7.0)',

        'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',

        'Windows ME' => 'Windows ME',

        'Open BSD' => 'OpenBSD',

        'Sun OS' => 'SunOS',

        'Linux' => '(Linux)|(X11)',

        'Mac OS' => '(Mac_PowerPC)|(Macintosh)',

        'QNX' => 'QNX',

        'BeOS' => 'BeOS',

        'OS/2' => 'OS/2',

        'Search Bot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'

);



// Loop through the array of user agents and matching operating systems

foreach($OSList as $CurrOS=>$Match)

{

        // Find a match

        if (eregi($Match, $_SERVER['HTTP_USER_AGENT']))

        {

                // We found the correct match

                break;

        }

}
// browser detection
$is_lynx = $is_gecko = $is_winIE = $is_macIE = $is_opera = $is_NS4 = $is_safari = $is_chrome = false;
 
if (strpos($_SERVER['HTTP_USER_AGENT'], 'Lynx') !== false) {
	$is_lynx = true;
      $a="lynx";
} elseif ( strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'chrome') !== false ) {
	$is_chrome = true;
      $a="chrome";
} elseif ( strpos(strtolower($_SERVER['HTTP_USER_AGENT']), 'webkit') !== false ) {
	$is_safari = true;
      $a="safari";
} elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'Gecko') !== false) {
	$is_gecko = true;
      $a="firefox";
} elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false && strpos($_SERVER['HTTP_USER_AGENT'], 'Win') !== false) {
	$is_winIE = true;
      $a="win_IE";
} elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false && strpos($_SERVER['HTTP_USER_AGENT'], 'Mac') !== false) {
	$is_macIE = true;
      $a="mac_IE";
} elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'Opera') !== false) {
	$is_opera = true;
      $a="opera";
} elseif (strpos($_SERVER['HTTP_USER_AGENT'], 'Nav') !== false && strpos($_SERVER['HTTP_USER_AGENT'], 'Mozilla/4.') !== false) {
	$is_NS4 = true;
      $a="netscape";
}
 
$is_IE = ( $is_macIE || $is_winIE );

$OSList = array
(

        // Match user agent string with operating systems

        'Windows 3.11' => 'Win16',

        'Windows 95' => '(Windows 95)|(Win95)|(Windows_95)',

        'Windows 98' => '(Windows 98)|(Win98)',

        'Windows 2000' => '(Windows NT 5.0)|(Windows 2000)',

        'Windows XP' => '(Windows NT 5.1)|(Windows XP)',

        'Windows Server 2003' => '(Windows NT 5.2)',

        'Windows Vista' => '(Windows NT 6.0)',

        'Windows 7' => '(Windows NT 7.0)',

        'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',

        'Windows ME' => 'Windows ME',

        'Open BSD' => 'OpenBSD',

        'Sun OS' => 'SunOS',

        'Linux' => '(Linux)|(X11)',

        'Mac OS' => '(Mac_PowerPC)|(Macintosh)',

        'QNX' => 'QNX',

        'BeOS' => 'BeOS',

        'OS/2' => 'OS/2',

        'Search Bot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'

);



// Loop through the array of user agents and matching operating systems

foreach($OSList as $CurrOS=>$Match)

{

        // Find a match

        if (eregi($Match, $_SERVER['HTTP_USER_AGENT']))

        {

                // We found the correct match

                break;

        }

}

Thats brilliant. Thanks :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.