phpdeprecatederegi

convert eregi to php 5.3


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


Solution

  • 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.