0

Does anyone know how I would convert the following code to php 5.3?

if (eregi('^(' . $value . ')(;q=[0-9]\\.[0-9])?$', $this->browser_languages[$i]) && isset($this->catalog_languages[$key])) {

Thanks

-James

jimbeeer
  • 1,604
  • 2
  • 12
  • 25

2 Answers2

1

preg_match() with the i (PCRE_CASELESS) modifier is the suggested alternative.

Bud Damyanov
  • 30,171
  • 6
  • 44
  • 52
0

The ereg functions became deprecated, replace them with the PCRE functions

http://www.php.net/manual/en/ref.pcre.php

http://php.net/manual/en/reference.pcre.pattern.posix.php

Edit :

To change from eregi to preg_match, you need to choose a character that will server as a delimiter (I often choose #) and add after the delimiter an i flag (wich means case insensitive).

You example :

if (eregi('^(' . $value . ')(;q=[0-9]\\.[0-9])?$', $this->browser_languages[$i])
    && isset($this->catalog_languages[$key])) {}

Will become :

if (preg_match('#^(' . $value . ')(;q=[0-9]\\.[0-9])?$#i', $this->browser_languages[$i]) 
    && isset($this->catalog_languages[$key])) {}

This is the minimum you need (note that you may need to use preg_quote() for the $value but I didn't add it to note complicate things for now)

another note is that if you want to convert ereg (not eregi), you don't need to add the i flag.

Oussama Jilal
  • 7,669
  • 2
  • 30
  • 53
  • Thanks, but I'm still absolutely no wiser as to what to convert it to as I don't really understand what the eregi is actually doing. – jimbeeer Mar 28 '13 at 10:34
  • eregi is a function for matching regular expressions, if you don't know what regular expressions are, you can read a little about them here http://en.wikipedia.org/wiki/Regular_expression – Oussama Jilal Mar 28 '13 at 10:39
  • sorry for my curt response, but my situation is i've just moved a clients website over to a new server and it's bringing up that error on the home page. I've got to fix it as fast as I can before he notices. I'm not sure I deserve to be modded down as a result. – jimbeeer Mar 28 '13 at 10:45
  • See my edited answer (I hope you can spot the difference) – Oussama Jilal Mar 28 '13 at 11:18
  • Absolutely brilliant. Thanks very much. I tried changing it, but based on other answers on SO i was putting a slash in there, not a hash, plus i missed out the i at the end. I fully get the difference. You've saved my bacon and I'll now know the answer for next time! – jimbeeer Mar 28 '13 at 11:24
  • you can put a slash instead of hash, but if the regular expression contains a slash too, you will have to escape it with an anti-slash, a hash is less frequently used so that's why I use it (note that you can use any character not only slashes and hashes as a delimiter) – Oussama Jilal Mar 28 '13 at 11:37