Function eregi() is deprecated [duplicate]
Asked Answered
A

3

5

Function eregi() is deprecated. How can i replace eregi(). I try with preg_match but then stop working.

i us ethis help:

http://takien.com/513/how-to-fix-function-eregi-is-deprecated-in-php-5-3-0.php

CODE BEFORE:

if ( ! eregi("convert$", $this->library_path))
        {
            if ( ! eregi("/$", $this->library_path)) $this->library_path .= "/";

            $this->library_path .= 'convert';
        }

if (eregi("gd2$", $protocol))
        {
            $protocol = 'image_process_gd';
        }

CODE THEN:

if ( ! preg_match("convert$/i", $this->library_path))
        {
            if ( ! preg_match("/$/i", $this->library_path)) $this->library_path .= "/";

            $this->library_path .= 'convert';
        }

if (preg_match("gd2$/i", $protocol))
        {
            $protocol = 'image_process_gd';
        }
Aloes answered 18/4, 2011 at 9:18 Comment(1)
PHP is moving toward PCRE extension : php.net/manual/en/function.eregi.phpDigitate
C
9

preg_match expects its regex argument to be within a pair delimiters.

So try:

if ( ! preg_match("#convert$#i", $this->library_path)) {
        if ( ! preg_match("#/$#i", $this->library_path)) 
                $this->library_path .= "/";

        $this->library_path .= 'convert';
}

if (preg_match("#gd2$#i", $protocol)) {                                         
        $protocol = 'image_process_gd'; 
}     
Carnassial answered 18/4, 2011 at 9:22 Comment(1)
No need for this particular task to be completed with regular expressions..,Phocomelia
B
2

It seems you just forgot the Delimiter

preg_match("~/$~", $this->library_path)

and

preg_match("~gd2$~i", $protocol)

But in both cases you should consider not using regular expressions, because they are oversized here

$this->library_path[strlen($this->library_path) - 1] == '/'
substr($protocol, -3) == 'gd2'
Balf answered 18/4, 2011 at 9:20 Comment(0)
P
1

If you are just checking for the presence of one string inside another, you should just use the strpos() function. eg:

if(strpos('convert', $this->library_path) !== false) {
    // code here
}

Update: Misread you want to check for it at the END of a string, this is still possible without Regex by using substr():

if(substr($this->library_path, -7) == 'convert'  {
    //code here
}

Where 7 is the length of convert, you could use a strlen and subtract it from 0 to get this number dynamically. This won't start any regex so is much more efficient.

Phocomelia answered 18/4, 2011 at 9:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.