Follow up to Francesco R's post from 2016.
His function works for most human traffic; added a few lines to cover the most common bot traffic. Also fixed issue with function failing to detect strings at position 0 due to strpos behavior.
<?php
function get_browser_name($user_agent)
{
$t = strtolower($user_agent);
$t = " " . $t;
if (strpos($t, 'opera' ) || strpos($t, 'opr/') ) return 'Opera' ;
elseif (strpos($t, 'edge' ) ) return 'Edge' ;
elseif (strpos($t, 'chrome' ) ) return 'Chrome' ;
elseif (strpos($t, 'safari' ) ) return 'Safari' ;
elseif (strpos($t, 'firefox' ) ) return 'Firefox' ;
elseif (strpos($t, 'msie' ) || strpos($t, 'trident/7')) return 'Internet Explorer';
elseif (strpos($t, 'google' ) ) return '[Bot] Googlebot' ;
elseif (strpos($t, 'bing' ) ) return '[Bot] Bingbot' ;
elseif (strpos($t, 'slurp' ) ) return '[Bot] Yahoo! Slurp';
elseif (strpos($t, 'duckduckgo') ) return '[Bot] DuckDuckBot' ;
elseif (strpos($t, 'baidu' ) ) return '[Bot] Baidu' ;
elseif (strpos($t, 'yandex' ) ) return '[Bot] Yandex' ;
elseif (strpos($t, 'sogou' ) ) return '[Bot] Sogou' ;
elseif (strpos($t, 'exabot' ) ) return '[Bot] Exabot' ;
elseif (strpos($t, 'msn' ) ) return '[Bot] MSN' ;
elseif (strpos($t, 'mj12bot' ) ) return '[Bot] Majestic' ;
elseif (strpos($t, 'ahrefs' ) ) return '[Bot] Ahrefs' ;
elseif (strpos($t, 'semrush' ) ) return '[Bot] SEMRush' ;
elseif (strpos($t, 'rogerbot' ) || strpos($t, 'dotbot') ) return '[Bot] Moz or OpenSiteExplorer';
elseif (strpos($t, 'frog' ) || strpos($t, 'screaming')) return '[Bot] Screaming Frog';
elseif (strpos($t, 'facebook' ) ) return '[Bot] Facebook' ;
elseif (strpos($t, 'pinterest' ) ) return '[Bot] Pinterest' ;
elseif (strpos($t, 'crawler' ) || strpos($t, 'api' ) ||
strpos($t, 'spider' ) || strpos($t, 'http' ) ||
strpos($t, 'bot' ) || strpos($t, 'archive') ||
strpos($t, 'info' ) || strpos($t, 'data' ) ) return '[Bot] Other' ;
return 'Other (Unknown)';
}
?>
Post with more depth here:
https://www.256kilobytes.com/content/show/1922/how-to-parse-a-user-agent-in-php-with-minimal-effort