【问题标题】:PHP Warning: preg_match(): Unknown modifier [duplicate]PHP警告:preg_match():未知修饰符[重复]
【发布时间】:2011-09-26 22:02:26
【问题描述】:

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

PHP 警告:preg_match() [function.preg-match]:第 292 行 /index.php 中的未知修饰符“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,但仍然重定向到 MSIE 8 与 Windows XP 的错误页面

【问题讨论】:

  • 忘记在最后一行添加 'Search Bot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp/cat)|(msnbot)|(ia_archiver )'

标签: php windows internet-explorer internet-explorer-8 windows-xp


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    OS\/2 这样在OS/2 中转义斜线应该可以解决它。

    【讨论】:

    • os 数组中的大部分值都是模式。使用 preg_quote 将不正确地转义这些模式。
    • 谢谢,我查看的是索引而不是值,更新了答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-20
    • 2011-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-01
    相关资源
    最近更新 更多