Display only 1 element of a template var

Hello,
I have this piece of code for my phpBB board:

'USER_BROWSER'		=> $this->user->browser,

or:
'S_USER_BROWSER' => (isset($user->data['session_browser'])) ? $user->data['session_browser'] : $user->lang['UNKNOWN_BROWSER'],
Works semi fine, but displays way too much info:

Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36

Browser name and Browser version is all I need.
As far as I can understand, this is not as easy as it should be :smile:

Exactly how much of the value do you want?

A simple method would be to just get the part after the last ‘)’.

Php does have a function to do this, but it often doesn’t work. I just tried it with chrome and edge and it doesn’t return expected results.

I would use preg_match to get all the name/version values at the end, then use some conditional logic to get the correct value for the different browsers. Here’s a link that contains a current description for the major browsers - User-Agent - HTTP | MDN

Actually “Chrome” is enough.
But probably not that easy to do.
This is the Firefox string, not the same as Chrome: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:137.0) Gecko/20100101 Firefox/137.0

<?php
function getBrowser() {
    $userAgent = $_SERVER['HTTP_USER_AGENT'];
    $browser = "Unknown Browser";
    $version = "";

    // Check for common browsers
    if (preg_match('/Firefox\/([0-9.]+)/', $userAgent, $matches)) {
        $browser = 'Firefox';
        $version = $matches[1];
    } elseif (preg_match('/Chrome\/([0-9.]+)/', $userAgent, $matches)) {
        $browser = 'Chrome';
        $version = $matches[1];
    } elseif (preg_match('/Safari\/([0-9.]+)/', $userAgent, $matches)) {
        $browser = 'Safari';
        $version = $matches[1];
    } elseif (preg_match('/Edge\/([0-9.]+)/', $userAgent, $matches)) {
        $browser = 'Edge';
        $version = $matches[1];
    } elseif (preg_match('/MSIE ([0-9.]+)/', $userAgent, $matches)) {
        $browser = 'Internet Explorer';
        $version = $matches[1];
    }

    return array(
        'name' => $browser,
        'version' => $version
    );
}

// Usage example
$browserInfo = getBrowser();
echo "Browser: " . $browserInfo['name'] . "\n";
echo "Version: " . $browserInfo['version'];

I admit I cheated as I used AI. :rofl:

Unfortunately, no. This doesn’t identify or it misidentifies Opera, and Edge, because just finding those values in the strings doesn’t identify the browser.

For most of these client strings, the last entry is the identifying value. However, for Chrome, and a few mobile devices, it is the first value. Chrome and others also include Safari as the last entry. Safari only includes itself.

There are also variations in spelling, such as Edg, EdgA, and Edge all for Edge (microsoft doing their own thing again.) Opera is actually OPR.

I would do this positionally, by making an array of last entry ‘only’ values, that if found (in_array()) cause the last entry to be used. If the last entry ‘only’ comparison fails and Chrome is not found at all, use Safari. Otherwise use Chrome.

@Ulrik, what is your actual use for this? I hope it is only for display purposes or recording visits by client, because it isn’t exact and unless you make use of one of the browser client api’s, you will never be able to keep up with all the different values.

@phdr its for display only.
Not important in any way. Im just trying to figure out if there is a way to achieve it.
Its doesnt have to be 100% flawless, but I dont want to be forced out in some kind of complicated way to do it. Its not that important.

Primarily based on information for the latest user agent strings found at What are the latest user agents for popular web browsers? here’s what I came up with.

The rules -

  • if a name is Firefox or FxiOS, it is Firefox
  • if a name is Edg, EdgA, Edge, or EdgiOS (Ed*) it is Edge
  • if a name is OPR, it is Opera
  • if there is only one name and it is Safari, it is Safari
  • if a name is not any of the above and the first element is Chrome or CriOS, it is Chrome
  • if it is none of the above, it is an unknown browser

I did not include IE. I did not include Vivaldi, or Yandex (YaBrowser) - these two are uniquely named, like firefox, edge, and opera. If the name is found, it is that browser. It would be simple matter to add these to the code.

The code -

function getBrowser($ua)
{
	if(preg_match_all('/\b((?:Firefox|FxiOS|Ed[a-zA-Z]*|OPR|Presto|Safari|Chrome|CriOS))\/([a-zA-Z0-9.]+)/',$ua,$matches))
	{
		//echo '<pre>'; print_r($matches); echo '</pre>';

		if(in_array('Firefox',$matches[1]))
		{
			return 'Firefox';
		}
		if(in_array('FxiOS',$matches[1]))
		{
			return 'Firefox';
		}
		if(in_array('Edg',$matches[1]))
		{
			return 'Edge';
		}
		if(in_array('EdgA',$matches[1]))
		{
			return 'Edge';
		}
		if(in_array('Edge',$matches[1]))
		{
			return 'Edge';
		}
		if(in_array('EdgiOS',$matches[1]))
		{
			return 'Edge';
		}
		if(in_array('OPR',$matches[1]))
		{
			return 'Opera';
		}
		if(in_array('Presto',$matches[1]))
		{
			return 'Opera';
		}
		if(count($matches[1]) == 1 && in_array('Safari',$matches[1]))
		{
			return 'Safari';
		}
		if('Chrome' == $matches[1][0])
		{
			return 'Chrome';
		}
		if('CriOS' == $matches[1][0])
		{
			return 'Chrome';
		}
		return 'Unknown browser';
	}
	else
	{
		return 'No browser';
	}
}

This was tested with the example user agent strings, at the MDN link previously posted in this thread and at that the whatismybrowser link just posted in this reply.

Thanks for all the help. Did a some testing myself and ended up with this (some of your suggestions is just added)

protected function detect_browser($user_agent)
    {
        $browsers = ['SamsungBrowser', 'samsungbrowser', 'Edg', 'EdgA', 'Edge', 'OPR', 'Opera', 'Firefox', 'FxiOS', 'MSIE', 'Trident', 'ucbrowser', 'Chrome', 'CriOS', 'Safari'];

        foreach ($browsers as $browser) {
            if (stripos($user_agent, $browser) !== false) {
                if ($browser === 'Edg' || $browser === 'EdgA' || $browser === 'Edge' || $browser === 'EdgiOS') return 'Edge';
				if ($browser === 'OPR' || $browser === 'Opera') return 'Opera';
				if ($browser === 'CriOS' || $browser === 'Chrome') return 'Chrome';
				if ($browser === 'Firefox' || $browser === 'FxiOS') return 'Firefox';
                if ($browser === 'MSIE' || $browser === 'Trident') return 'Internet Explorer';
				if ($browser === 'SamsungBrowser' || $browser === 'samsungbrowser') return 'Samsung Internet';
				if ($browser === 'ucbrowser' || $browser === 'UCBROWSER') return 'UC Browser';
                return $browser;
            }
        }

        return 'Unknown';
    }
Sponsor our Newsletter | Privacy Policy | Terms of Service