title: PHP browser and os detection showing chrome instead of opera
body: <p>I have a simple php script to detect browser name and os. Everything goes well, except for OPERA, that shows as Chrome.
  Here is my code:</p>
  
  <pre><code>&lt;?php
  
  
  
      $user_agent = $_SERVER['HTTP_USER_AGENT'];
  
      function getOS() { 
  
          global $user_agent;
  
          $os_platform    =   'Unknown OS Platform';
  
          $os_array       =   array (
  
              '/windows nt 6.2/i'     =&gt; 'Windows 8',
              '/windows nt 6.1/i'     =&gt; 'Windows 7',
              '/windows nt 6.0/i'     =&gt; 'Windows Vista',
              '/windows nt 5.2/i'     =&gt; 'Windows Server 2003/XP x64',
              '/windows nt 5.1/i'     =&gt; 'Windows XP',
              '/windows xp/i'         =&gt; 'Windows XP',
              '/windows nt 5.0/i'     =&gt; 'Windows 2000',
              '/windows me/i'         =&gt; 'Windows ME',
              '/win98/i'              =&gt; 'Windows 98',
              '/win95/i'              =&gt; 'Windows 95',
              '/win16/i'              =&gt; 'Windows 3.11',
              '/macintosh|mac os x/i' =&gt; 'Mac OS X',
              '/mac_powerpc/i'        =&gt; 'Mac OS 9',
              '/linux/i'              =&gt; 'Linux',
              '/ubuntu/i'             =&gt; 'Ubuntu',
              '/iphone/i'             =&gt; 'iPhone',
              '/ipod/i'               =&gt; 'iPod',
              '/ipad/i'               =&gt; 'iPad',
              '/android/i'            =&gt; 'Android',
              '/blackberry/i'         =&gt; 'BlackBerry',
              '/webos/i'              =&gt; 'Mobile'
  
          );
  
          foreach ($os_array as $regex =&gt; $value) { 
  
              if (preg_match($regex, $user_agent)) $os_platform = $value;
  
          }   
  
          return $os_platform;
  
      }
  
  
  
      function getBrowser() {
  
          global $user_agent;
  
          $browser        =   "Unknown Browser";
  
          $browser_array  =   array (
  
              '/msie/i'       =&gt; 'Internet Explorer',
              '/firefox/i'    =&gt; 'Firefox',
              '/safari/i'     =&gt; 'Safari',
              '/chrome/i'     =&gt; 'Chrome',
              '/opera/i'      =&gt; 'Opera',
              '/netscape/i'   =&gt; 'Netscape',
              '/maxthon/i'    =&gt; 'Maxthon',
              '/konqueror/i'  =&gt; 'Konqueror',
              '/mobile/i'     =&gt; 'Handheld Browser'
  
          );
  
          foreach ($browser_array as $regex =&gt; $value) { 
  
              if (preg_match($regex, $user_agent)) $browser = $value;
  
          }
  
          return $browser;
  
      }
  
  
  
      $user_os        =   getOS();
      $user_browser   =   getBrowser();
  
      $device_details =   '&lt;strong&gt;Browser: &lt;/strong&gt;' . $user_browser . '&lt;br /&gt;&lt;strong&gt;Operating System: &lt;/strong&gt;' . $user_os;
  
      print_r($device_details);
  
      echo('&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;' . $_SERVER['HTTP_USER_AGENT']);
  
  
  
  ?&gt;
  </code></pre>
  
  <p>I changed the $browser_array by adding /OPR/i as another regex for opera and placed the Chrome regex after the 2 Opera regex's:</p>
  
  <pre><code>    $browser_array  =   array (
  
          '/msie/i'       =&gt; 'Internet Explorer',
          '/firefox/i'    =&gt; 'Firefox',
          '/safari/i'     =&gt; 'Safari',
          '/OPR/i'        =&gt; 'Opera',
          '/opera/i'      =&gt; 'Opera',
          '/chrome/i'     =&gt; 'Chrome',
          '/netscape/i'   =&gt; 'Netscape',
          '/maxthon/i'    =&gt; 'Maxthon',
          '/konqueror/i'  =&gt; 'Konqueror',
          '/mobile/i'     =&gt; 'Handheld Browser'
  
      );
  </code></pre>
  
  <p>here is Chrome UA:</p>
  
  <pre><code>Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.72 Safari/537.36
  </code></pre>
  
  <p>And here is Opera UA:</p>
  
  <pre><code>Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.71 Safari/537.36 OPR/15.0.1147.141
  </code></pre>