Viewed   101 times

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';
        }

 Answers

4

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'; 
}     
Friday, August 12, 2022
3

Why not have something like:

$reg = "/^(s*?)selects*?.*?s*?from([s]|[^;]|(['"].*;.*['"]))*?;s*?$/i";

works for the SQL select query examples: http://www.phpliveregex.com/p/6nP.

It also checks that the only SQL query being run is the select query, therefore it should only validate them. It does this by making sure that there is only one ; unless that ; is within a string, so the below will validate.

select * from users where id=1 AND name= 'Pra;bhu';

But this will not.

select * from users where id=1 AND name= 'Prabhu'; drop table;

And the regular expression which doesn't check for ; within a string and will fail if it is in it:

$reg = "/^(s*?)selects*?.*?s*?from([s]|[^;])*?;s*?$/i"
Wednesday, September 28, 2022
 
3

If a simple pattern would be sufficient depends on how your input could look like.

$re = '~Q$data['infos'][]E.*?);~s';
  • Q...E is used to match literally (also could escape the brackets/dollar).
  • .*? in single line mode (s flag) matches lazily any amount of any character.

See demo at regex101 or php demo at eval.in

Sunday, October 30, 2022
3

eregi() is deprecated as of PHP 5.3, use preg_match() instead.

Note that preg_match() is only case insensitive when you pass the i modifier in your regular expression.

include 'db_connect.php'; 
if(isset($_POST['Submit']))           
{            
    $acc_type=ucwords($_POST['acc_type']);
    $minbalance=ucwords($_POST['minbalance']);

    // Removed A-Z here, since the regular expression is case-insensitive                
    if (!preg_match("/^[a-z ]+$/i", stripslashes(trim($acc_type))))//line 20 
    {                 
        echo "Enter Valid Data for Account Type!";                
        exit(0);                 
    }           
    else 
    {                  
        // d and 0-9 do the same thing
        if (!preg_match("/^[d ]+$/", stripslashes(trim($minbalance))))//line 27
        {
        }
    }
} 
Thursday, October 20, 2022
2

You must use preg_match instead of ereg because the last one is deprecated.

Replacing it is not a big deal:

ereg( "[][{}()*+?.\^$|]", $_REQUEST['name'] )

will become:

preg_match( "/[][{}()*+?.\^$|]/", $_REQUEST['name'] )

p.s. I had to modify more than one hundred files while I was porting my old project to PHP 5.3 to avoid manually modifying I've used following script to do it for me:

function replaceEregWithPregMatch($path) {
    $content = file_get_contents($path);
    $content = preg_replace('/ereg(("|')(.+)("|'),/',
                            "preg_match('/$2/',",
                            $content);
    file_put_contents($path, $content);
}

I hope it helps.

Wednesday, August 10, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :