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:


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


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



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

Why not have something like:

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

works for the SQL select query examples:

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

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

Sunday, October 30, 2022

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

    // 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!";                
        // d and 0-9 do the same thing
        if (!preg_match("/^[d ]+$/", stripslashes(trim($minbalance))))//line 27
Thursday, October 20, 2022

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(("|')(.+)("|'),/',
    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 :