-1

i have this function, and i get this error Deprecated: Function eregi() is deprecated in.... If i change eregi to preg_match i get this error 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'; 
}

any ideas on how to fix this. and also i would like a dumbed down explanation on what is happening if you guys dont mind so i can understand

thanks

Patrioticcow
  • 26,422
  • 75
  • 217
  • 337
  • 2
    please point out why none of the 3939 search results for [deprecated+function+eregi](http://stackoverflow.com/search?q=Deprecated%3A+Function+eregi%28%29) helped solve your problem – Gordon Jul 23 '11 at 15:21

4 Answers4

3

You're supposed to use delimeters. Please read http://www.php.net/manual/en/reference.pcre.pattern.posix.php

In this case, the following should work:

if(preg_match('`'.$pattern.'`i', $userAgent))
duri
  • 14,991
  • 3
  • 44
  • 49
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.

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
1

Try

if(preg_match("#".$pattern."#", $userAgent)) {
marc
  • 6,103
  • 1
  • 28
  • 33
0

ereg and preg have different syntaxes. In this case, they are similar enough that you can simply add a delimiter around them and I think it would work.

troelskn
  • 115,121
  • 27
  • 131
  • 155