PHP 警告:preg_match():未知修饰符

发布于 2024-12-06 15:21:06 字数 1826 浏览 0 评论 0原文

我试图根据操作系统将用户重定向到不同的页面,但不断收到此警告:

PHP警告:preg_match()[function.preg-match]:/index.php第292行中的未知修饰符'2'

它似乎只是Windows NT 5.1 和 MSIE 8.0 浏览器配置中会发生这种情况:

function getOS($userAgent) {
  // Create list of operating systems with operating system name as array key 
    $oses = array (
        'iPhone' => '(iPhone)',
        'iPad' => 'iPad',
        'Android' => 'Android',
        'Windows 3.11' => 'Win16',
        'Windows 95' => '(Windows 95)|(Win95)|(Windows_95)', // Use regular expressions as value to identify operating system
        'Windows 98' => '(Windows 98)|(Win98)',
        'Windows 2000' => '(Windows NT 5.0)|(Windows 2000)',
        'Windows XP' => '(Windows NT 5.1)|(Windows XP)',
        'Windows 2003' => '(Windows NT 5.2)',
        'Windows Vista' => '(Windows NT 6.0)|(Windows Vista)',
        'Windows 7' => '(Windows NT 6.1)|(Windows 7)',
        'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
        'Windows ME' => 'Windows ME',
        'Blackberry' => 'Blackberry',
        'Open BSD'=>'OpenBSD',
        'Sun OS'=>'SunOS',
        'Linux'=>'(Linux)|(X11)',

        'Macintosh'=>'(Mac_PowerPC)|(Macintosh)',
        'QNX'=>'QNX',
        'BeOS'=>'BeOS',
        'OS/2'=>'OS/2',
        'Search Bot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp/cat)|(msnbot)|(ia_archiver)'
    );
    //'Safari' => '(Safari)',
foreach($oses as $os=>$pattern){ // Loop through $oses array
    // Use regular expressions to check operating system type

    if(preg_match("/".$pattern."/i", $userAgent)) { 
            // Check if a value in $oses array matches current 
            //user agent. <---- Line 292

尝试删除 OS/2 并更改为 OS2,但在 Windows XP 中仍然重定向到 MSIE 8 的错误页面

I'm trying to redirect users to different pages based on OS but keep getting this warning:

PHP Warning: preg_match() [function.preg-match]: Unknown modifier '2' in /index.php on line 292

It only seems to happen with Windows NT 5.1 and MSIE 8.0 browser configuration:

function getOS($userAgent) {
  // Create list of operating systems with operating system name as array key 
    $oses = array (
        'iPhone' => '(iPhone)',
        'iPad' => 'iPad',
        'Android' => 'Android',
        'Windows 3.11' => 'Win16',
        'Windows 95' => '(Windows 95)|(Win95)|(Windows_95)', // Use regular expressions as value to identify operating system
        'Windows 98' => '(Windows 98)|(Win98)',
        'Windows 2000' => '(Windows NT 5.0)|(Windows 2000)',
        'Windows XP' => '(Windows NT 5.1)|(Windows XP)',
        'Windows 2003' => '(Windows NT 5.2)',
        'Windows Vista' => '(Windows NT 6.0)|(Windows Vista)',
        'Windows 7' => '(Windows NT 6.1)|(Windows 7)',
        'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
        'Windows ME' => 'Windows ME',
        'Blackberry' => 'Blackberry',
        'Open BSD'=>'OpenBSD',
        'Sun OS'=>'SunOS',
        'Linux'=>'(Linux)|(X11)',

        'Macintosh'=>'(Mac_PowerPC)|(Macintosh)',
        'QNX'=>'QNX',
        'BeOS'=>'BeOS',
        'OS/2'=>'OS/2',
        'Search Bot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp/cat)|(msnbot)|(ia_archiver)'
    );
    //'Safari' => '(Safari)',
foreach($oses as $os=>$pattern){ // Loop through $oses array
    // Use regular expressions to check operating system type

    if(preg_match("/".$pattern."/i", $userAgent)) { 
            // Check if a value in $oses array matches current 
            //user agent. <---- Line 292

Tried removing OS/2 and changing to OS2 but still redirects to the wrong page for MSIE 8 with Windows XP

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

彩虹直至黑白 2024-12-13 15:21:06

问题在于 OS/2 并使用 / 作为分隔符。您可以像 OS\/2 一样转义正斜杠,它应该可以工作。

problem is with OS/2 and using / as your delimeter. You can just escape the forwardslash like OS\/2 and it should work.

放手` 2024-12-13 15:21:06

我的猜测是它发生在数组中的 OS/2 值上。由于您使用 / 作为分隔符,因此它在 2 之前结束,并且它认为 2 是像 i(不区分大小写)或 s(点匹配全部)这样的修饰符。

像这样 OS\/2 一样转义 OS/2 中的斜杠应该可以修复它。

My guess is that its happening with the OS/2 value in your array. Since you used / as your delimiter, it is ending before the 2 and it thinks the 2 is a modifier like i (case insensitive) or s (dot match all).

Escaping the slash in OS/2 like this OS\/2 should fix it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文