-1

我有这个功能,我得到这个错误Deprecated: Function eregi() is deprecated in...。如果我更改eregipreg_match我收到此错误Warning: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash in...

function getBrowser($userAgent) {
$browsers = array(
    'Opera' => 'Opera',
    'Mozilla Firefox'=> '(Firebird)|(Firefox)', // Use regular expressions as value to identify browser
    'Galeon' => 'Galeon',
    'Chrome'=>'Gecko',
    'MyIE'=>'MyIE',
    'Lynx' => 'Lynx',
    'Netscape' => '(Mozilla/4\.75)|(Netscape6)|(Mozilla/4\.08)|(Mozilla/4\.5)|(Mozilla/4\.6)|(Mozilla/4\.79)',
    'Konqueror'=>'Konqueror',
    'SearchBot' => '(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp/cat)|(msnbot)|(ia_archiver)',
    'Internet Explorer 8' => '(MSIE 8\.[0-9]+)',
    'Internet Explorer 7' => '(MSIE 7\.[0-9]+)',
    'Internet Explorer 6' => '(MSIE 6\.[0-9]+)',
    'Internet Explorer 5' => '(MSIE 5\.[0-9]+)',
    'Internet Explorer 4' => '(MSIE 4\.[0-9]+)',
);
foreach($browsers as $browser=>$pattern) {
    if(eregi($pattern, $userAgent)) {
        return $browser; 
    }
}
return 'Unknown'; 
}

有想法该怎么解决这个吗。如果你们不介意的话,我也想对正在发生的事情做一个简单的解释,这样我就能理解

谢谢

4

4 回答 4

3

你应该使用分隔符。请阅读http://www.php.net/manual/en/reference.pcre.pattern.posix.php

在这种情况下,以下应该起作用:

if(preg_match('`'.$pattern.'`i', $userAgent))
于 2011-07-23T15:11:12.930 回答
1

ereg() is the old way of doing regular expressions in PHP, and PCRE (preg_match and other preg_*) are faster and more powerful -- which explains why the first one is now deprecated.

Migrating from ereg to PCRE should not be too hard, but there are a couple of differences between the syntaxes accepted by those two engines -- which means you'll probably have to fix several of your regular expressions.


Here, the difference that explains the message you get is that PCRE expects a delimiter arround the regex.

For example, your regex should not be Galeon, but /Galeon/
And you can use pretty much any character you like as delimiter.


For more informations :

Quoting the first point of that last link :

The PCRE functions require that the pattern is enclosed by delimiters.

于 2011-07-23T15:11:49.323 回答
1

Try

if(preg_match("#".$pattern."#", $userAgent)) {
于 2011-07-23T15:13:19.250 回答
0

ereg 和 preg 有不同的语法。在这种情况下,它们足够相似,您可以简单地在它们周围添加一个分隔符,我认为它会起作用。

于 2011-07-23T15:11:16.050 回答