P
P
Puristaako2014-11-29 20:05:06
PHP
Puristaako, 2014-11-29 20:05:06

Where are the errors preg_match() [function.preg-match]: Unknown modifier '1' in the function?

The function is not mine, I honestly copied it, but for the second day I can’t understand why errors are coming out (although the output always works). I studied all the features of the functions used on php.net, climbed a bunch of sites. Tried preg_quote($pattern, '|') as advised for similar problems. Already redone without an array, just search for keywords in USER_AGENT. But you want to know what's wrong?
Here is the function itself:

function getOS($user_agent) {
  $oses = array(
    // Mircrosoft Windows 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 2000 Service Pack 1' => '(Windows NT 5.01)',
    'Windows XP' => '(Windows NT 5.1)|(Windows XP)',
    'Windows Server 2003' => '(Windows NT 5.2)',
    'Windows Vista' => '(Windows NT 6.0)|(Windows Vista)',
    'Windows 7' => '(Windows NT 6.1)|(Windows 7)',
    'Windows 8' => '(Windows NT 6.2)|(Windows 8)',
    'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
    'Windows ME' => '(Windows ME)|(Windows 98; Win 9x 4.90 )',
    'Windows CE' => '(Windows CE)',
    // UNIX Like Operating Systems
    'Mac OS X Kodiak (beta)' => '(Mac OS X beta)',
    'Mac OS X Cheetah' => '(Mac OS X 10.0)',
    'Mac OS X Puma' => '(Mac OS X 10.1)',
    'Mac OS X Jaguar' => '(Mac OS X 10.2)',
    'Mac OS X Panther' => '(Mac OS X 10.3)',
    'Mac OS X Tiger' => '(Mac OS X 10.4)',
    'Mac OS X Leopard' => '(Mac OS X 10.5)',
    'Mac OS X Snow Leopard' => '(Mac OS X 10.6)',
    'Mac OS X Lion' => '(Mac OS X 10.7)',
    'Mac OS X' => '(Mac OS X)',
    'Mac OS' => '(Mac_PowerPC)|(PowerPC)|(Macintosh)',
    'Open BSD' => '(OpenBSD)',
    'SunOS' => '(SunOS)',
    'Solaris 11' => '(Solaris/11)|(Solaris11)',
    'Solaris 10' => '((Solaris/10)|(Solaris10))',
    'Solaris 9' => '((Solaris/9)|(Solaris9))',
    'CentOS' => '(CentOS)',
    'QNX' => '(QNX)',
    // Kernels
    'UNIX' => '(UNIX)',
    // Linux Operating Systems
    'Ubuntu 12.10' => '(Ubuntu/12.10)|(Ubuntu 12.10)',
    'Ubuntu 12.04 LTS' => '(Ubuntu/12.04)|(Ubuntu 12.04)',
    'Ubuntu 11.10' => '(Ubuntu/11.10)|(Ubuntu 11.10)',
    'Ubuntu 11.04' => '(Ubuntu/11.04)|(Ubuntu 11.04)',
    'Ubuntu 10.10' => '(Ubuntu/10.10)|(Ubuntu 10.10)',
    'Ubuntu 10.04 LTS' => '(Ubuntu/10.04)|(Ubuntu 10.04)',
    'Ubuntu 9.10' => '(Ubuntu/9.10)|(Ubuntu 9.10)',
    'Ubuntu 9.04' => '(Ubuntu/9.04)|(Ubuntu 9.04)',
    'Ubuntu 8.10' => '(Ubuntu/8.10)|(Ubuntu 8.10)',
    'Ubuntu 8.04 LTS' => '(Ubuntu/8.04)|(Ubuntu 8.04)',
    'Ubuntu 6.06 LTS' => '(Ubuntu/6.06)|(Ubuntu 6.06)',
    'Red Hat Linux' => '(Red Hat)',
    'Red Hat Enterprise Linux' => '(Red Hat Enterprise)',
    'Fedora 17' => '(Fedora/17)|(Fedora 17)',
    'Fedora 16' => '(Fedora/16)|(Fedora 16)',
    'Fedora 15' => '(Fedora/15)|(Fedora 15)',
    'Fedora 14' => '(Fedora/14)|(Fedora 14)',
    'Chromium OS' => '(ChromiumOS)',
    'Google Chrome OS' => '(ChromeOS)',
    // Kernel
    'Linux' => '(Linux)|(X11)',
    // BSD Operating Systems
    'OpenBSD' => '(OpenBSD)',
    'FreeBSD' => '(FreeBSD)',
    'NetBSD' => '(NetBSD)',
    // Mobile Devices
    'Andriod' => '(Android)',
    'iPod' => '(iPod)',
    'iPhone' => '(iPhone)',
    'iPad' => '(iPad)',
    //DEC Operating Systems
    'OS/8' => '(OS/8)|(OS8)',
    'Older DEC OS' => '(DEC)|(RSTS)|(RSTS/E)',
    'WPS-8' => '(WPS-8)|(WPS8)',
    // BeOS Like Operating Systems
    'BeOS' => '(BeOS)|(BeOS r5)',
    'BeIA' => '(BeIA)',
    // OS/2 Operating Systems
    'OS/2 2.0' => '(OS/220)|(OS/2 2.0)',
    'OS/2' => '(OS/2)|(OS2)',
    // Search engines
    'Search engine or robot' => '(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(msnbot)|(Ask Jeeves/Teoma)|(ia_archiver)'
  );
        
        foreach ($oses as $os => $pattern) {
    if (preg_match("/$pattern/i", $user_agent)) {
      return $os;
    }
  }
  return 'Unknown';
}

With this HTTP_USER_AGENT everything is fine
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36
And for example with this...
Mozilla/5.0 (Linux; Android 4.1. 2; Nexus 7 Build/JZ054K) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Safari/535.19
and some others a bunch of:
Warning: preg_match() [function.preg-match]: Unknown modifier '1' in /home/whitehouse.org/test.php on line 114
Warning: preg_match() [function.preg-match]: Unknown modifier '1' in /home/whitehouse.org/test.php on line 114

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rsa97, 2014-11-29
@Rsa97

The problem is the '/' character - it is perceived as the end of the pattern. Replace it with '\/', then it will work correctly.

T
toxa82, 2014-11-30
@toxa82

Check out this feature , maybe it will suit you better than regular expressions.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question