PHP error - Deprecated: Function ereg_replace() is deprecated

I am receiving the following error message and would like to know if I should replace all occurrences of ereg_replace with preg_replace in my code and do I need to change anything else?

Deprecated: Function ereg_replace() is deprecated in /home/mywebsite/public_html/admin/website_settings.php on line 797
$imagesml = ereg_replace ( '.jpg', '_med.jpg',  $row_view_tbl["image1"]);
$temps[$j] = ereg_replace ( '.jpg', '_sml.jpg', $temp[$j]);

Thanks in adavance for you feedback.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Terry WoodsIT GuruCommented:
Yes, they need to be changed, but it's not as simple as changing the function name. How many occurrences do you have? It's pretty quick for me to convert them:

$imagesml = ereg_replace ( '.jpg', '_med.jpg',  $row_view_tbl["image1"]);
$imagesml = preg_replace ( '/\.jpg/', '_med.jpg',  $row_view_tbl["image1"]); #Escaped the . to treat it as a literal period rather than a wilcard

$temps[$j] = ereg_replace ( '.jpg', '_sml.jpg', $temp[$j]);
$temps[$j] = preg_replace ( '/\.jpg/', '_sml.jpg', $temp[$j]); #Escaped the . to treat it as a literal period rather than a wilcard

Note the eregi_replace function is affected too.
Ray PaseurCommented:
I am not recommending this as  along-term strategy, but you can also turn off the Deprecated messages.  That might be a good solution short term.

Add this to the top of your script:
error_reporting(E_ALL ^ E_DEPRECATED);

Open in new window

Ray PaseurCommented:
Since this message is occurring as a byproduct of a PHP upgrade migration, you may want to know about other PHP upgrade issues.  These articles cover a couple of the issues.

PHP also has migration guides on the web site.  See the pages linked from this (left sidebar).
OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

sabecsAuthor Commented:
Thanks for your feedback, I probably have about 100 occurrences throughout my website.
If I set  error_reporting to hide the messages I am assuming that the ereg_replace will still work, is that correct and if so for how long?
Ray PaseurCommented:
Yes, that is probably correct.  And it is correct until it is no longer correct.  In other words, the period of success is unpredictable and you want to fix the issue ASAP.

Here is what I have experienced.  My host upgraded PHP.  I started getting a lot of awful messages.  I turned the "deprecated" messages off.  I used a code scanner to find all of the ereg() related function calls.  I corrected the lines with these functions (there were hundreds).  After I corrected each of the deprecated function calls, I tested the scripts by setting up a test case with deprecated messages set to "on" and once the test cases were satisfied, I committed the script revisions.  In my experience with a thousand-script site, this took about a full day but I still had time to go to lunch.

YMMV, and I wish you the best of luck, ~Ray
Terry WoodsIT GuruCommented:
100 occurrences doesn't take long to convert - feel free to post them.
sabecsAuthor Commented:
Thanks for your help, much appreciated.
I have about 100 occurrences but most are the same repeated throught the site so really I just have 5 similar ones which are shown below.

$imagesml = "/".ereg_replace ( '.jpg', '_swg.jpg',  $row_view_photos1["image1"]);
$image1 = ereg_replace ( '.jpg', '_swg.jpg',  $image1);
$imagesml1 = ereg_replace ( '.jpg', '_sml.jpg',  $row_view_tbl["image1"]);
$release = ereg_replace('^([[:digit:]]+\.[[:digit:]]+).*', '\1', $parts[2]);
$release = ereg_replace('-.*', '', $parts[2]);
Terry WoodsIT GuruCommented:
$imagesml = "/".ereg_replace ( '.jpg', '_swg.jpg',  $row_view_photos1["image1"]);
$imagesml = "/".preg_replace ( '/\.jpg/', '_swg.jpg',  $row_view_photos1["image1"]); # requires a period before the jpg extension
$image1 = ereg_replace ( '.jpg', '_swg.jpg',  $image1);
$image1 = preg_replace ( '/\.jpg/', '_swg.jpg',  $image1); # requires a period before the jpg extension
$imagesml1 = ereg_replace ( '.jpg', '_sml.jpg',  $row_view_tbl["image1"]);
$imagesml1 = preg_replace ( '/\.jpg/', '_sml.jpg',  $row_view_tbl["image1"]); # requires a period before the jpg extension
$release = ereg_replace('^([[:digit:]]+\.[[:digit:]]+).*', '\1', $parts[2]);
$release = preg_replace('/^(\d+\.\d+).*/', '\1', $parts[2]);
$release = ereg_replace('-.*', '', $parts[2]);
$release = preg_replace('/-.*/', '', $parts[2]);

Open in new window

Where I've added a comment, I've actually altered the pattern functionality slightly - because the . character is a wildcard, this code:
ereg_replace ( '.jpg', '_swg.jpg',  $image1);
would have changed the value "foodjpg" to "foo_swg.jpg". ie it didn't actually require a . character in the filename. I changed the . to \. so that the character is treated literally rather than as a wildcard.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.