PHP 警告:preg_match():未知修饰符
我试图根据操作系统将用户重定向到不同的页面,但不断收到此警告:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题在于
OS/2
并使用/
作为分隔符。您可以像OS\/2
一样转义正斜杠,它应该可以工作。problem is with
OS/2
and using/
as your delimeter. You can just escape the forwardslash likeOS\/2
and it should work.我的猜测是它发生在数组中的
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 thisOS\/2
should fix it.