33

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.

zzlalani
  • 21,360
  • 16
  • 42
  • 72
Pradip
  • 1,287
  • 4
  • 21
  • 35
  • A Reference Question is: [How can I convert ereg expressions to preg in PHP?](http://stackoverflow.com/q/6270004/367456) – hakre Nov 16 '12 at 09:12

6 Answers6

44

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

hakre
  • 184,866
  • 48
  • 414
  • 792
Quentin
  • 857,932
  • 118
  • 1,152
  • 1,264
35
print $input."<hr>".ereg_replace('/&/', ':::', $input);

becomes

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

More example :

$mytext = ereg_replace('[^A-Za-z0-9_]', '', $mytext );

is changed to

$mytext = preg_replace('/[^A-Za-z0-9_]/', '', $mytext );
Sumit Bijvani
  • 8,012
  • 17
  • 49
  • 82
6

change the call to ereg_replace to use preg_replace instead

Mark Baker
  • 205,174
  • 31
  • 336
  • 380
4

http://php.net/ereg_replace says:

Note: As of PHP 5.3.0, the regex extension is deprecated in favor of the PCRE extension.

Thus, preg_replace is in every way better choice. Note there are some differences in pattern syntax though.

Amadan
  • 179,482
  • 20
  • 216
  • 275
3

IIRC they suggest using the preg_ functions instead (in this case, preg_replace).

Wevah
  • 28,020
  • 7
  • 84
  • 72
3

Here is more information regarding replacing ereg_replace with preg_replace

Darko Kenda
  • 4,651
  • 1
  • 26
  • 31