Function eregi_replace is deprecated in PHP

– post by Aravind

We are using a search engine called Search Plugin .  This search engine uses PHP code that was working well on its old server which had PHP 5.1 on it.

Recently, we migrated Search Plugin to one of our semi-dedicated server for enhancing its usability. After we migrated to this server, we faced the following error.

Deprecated: Function eregi_replace() is deprecated in /home/searchpl/public_html/include/searchfuncs.php on line 582

We came to knew recently that the in-built function mentioned in the error eregi_replace() is deprecated (or removed) in PHP 5.3.2. The recommendation from PHP is to use the alternate function preg_match.

Please see the sample code below

Old version: $a = strtolower(eregi_replace(“[ ]+”, ” “, $a));
New version: $a = strtolower(preg_replace(“/[ ]+/”, ” “, $a));

Please note that in preg_replace, the first argument should be placed between  “/” tags.

If you use “eregi” then the equivalent replacement will be “preg_match”
Sample code is

old version: while (eregi(“([-]?)\”([^\”]+)\””, $a, $regs)) {
New version: while (preg_match(“/([-]?)\”([^\”]+)/”, $a, $regs)) {

This replacements solved our function deprecated issue. I thought of sharing this with everyone. Since I am not a experienced developer, I am learning about the arguments in the function preg_replace and preg_match and also, the use of “/” in the first argument.

I will post my next updates after my learning, If any knows please comment here.

Thanks for reading this..