Asked  7 Months ago    Answers:  5   Viewed   74 times

I have written following PHP code:

$input="menu=1&type=0&";

print $input."<hr>".ereg_replace('/&/', ':::', $input);

After running above code, it gives following warning,

Deprecated: Function ereg_replace() is deprecated

How can I resolve this warning.

 Answers

20

Switch to preg_replaceDocs and update the expression to use preg syntax (PCRE) instead of ereg syntax (POSIX) where there are differencesDocs (just as it says to do in the manual for ereg_replaceDocs).

Wednesday, March 31, 2021
 
Taptronic
answered 7 Months ago
36

eregi() function has been DEPRECATED as of PHP 5.3.0. Relying on this feature is highly discouraged.

you can use preg_match().

Saturday, May 29, 2021
 
Szenis
answered 5 Months ago
22

I think you want preg_split.

list($year, $month, $day, $hour, $min, $sec) = preg_split('/[: -]/', $post_timestamp);
Friday, June 11, 2021
 
DCD
answered 5 Months ago
DCD
58

Use preg_replace instead, just add delimiters.

$row[$j] = preg_replace("#n#", "\n", $row[$j]);
Saturday, July 31, 2021
 
PiyaModi
answered 3 Months ago
77

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.

Monday, August 23, 2021
 
Aniket Thakur
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :