PHP 警告:preg_match() [function.preg-match]:未知修饰符

发布于 2024-12-06 09:51:45 字数 1809 浏览 0 评论 0原文

任何人都知道为什么会出现这种情况?尝试使用它来根据用户代理重定向用户。当用户代理是搜索机器人时,会出现此警告。此外,Windows XP MSIE 8 用户代理也被错误重定向。

“警告:preg_match() [function.preg-match]:未知修饰符“c” /.../getos.php 第 36 行”

function getOS($userAgent) {
  $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',
    'OS2'=>'OS2',
    '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 36
      return $os; // Operating system was matched so return $oses key
    }
  }
  return 'Unknown'; // Cannot find operating system so return Unknown
}

$operatingsystem = getOS($_SERVER['HTTP_USER_AGENT']);

Anyone have an idea of why this is popping up? Trying to use this to redirect users based on user agent. This warning comes up when user agent is search bot. Also Windows XP MSIE 8 user agent is being incorrectly redirected.

"Warning: preg_match() [function.preg-match]: Unknown modifier 'c' in
/.../getos.php on line 36"

function getOS($userAgent) {
  $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',
    'OS2'=>'OS2',
    '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 36
      return $os; // Operating system was matched so return $oses key
    }
  }
  return 'Unknown'; // Cannot find operating system so return Unknown
}

$operatingsystem = getOS($_SERVER['HTTP_USER_AGENT']);

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

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

发布评论

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

评论(1

秋叶绚丽 2024-12-13 09:51:45

您的 Search Bot 条目中包含 (Slurp/cat)/ 被计为正则表达式的结尾,后续的 c 会导致错误。

(Slurp\/cat) 将解决这个问题。

Your Search Bot entry has (Slurp/cat) in it. The / is being counted as the end of the regex, and the subsequent c causes the error.

(Slurp\/cat) will solve the problem.

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