PHP 警告:preg_match() [function.preg-match]:未知修饰符
任何人都知道为什么会出现这种情况?尝试使用它来根据用户代理重定向用户。当用户代理是搜索机器人时,会出现此警告。此外,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
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 subsequentc
causes the error.(Slurp\/cat)
will solve the problem.