Solved

PHP error - Deprecated: Function ereg_replace() is deprecated

Posted on 2012-03-21
8
518 Views
Last Modified: 2012-03-22
Hi,
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.
0
Comment
Question by:sabecs
  • 3
  • 3
  • 2
8 Comments
 
LVL 35

Expert Comment

by:Terry Woods
Comment Utility
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.
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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

0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_6630-Magic-Quotes-a-bad-idea-from-day-one.html
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_7317-Register-Globals-a-bad-idea-from-day-one.html

PHP also has migration guides on the web site.  See the pages linked from this (left sidebar).
http://us.php.net/migration54
0
 

Author Comment

by:sabecs
Comment Utility
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?
0
Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 100 total points
Comment Utility
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
0
 
LVL 35

Expert Comment

by:Terry Woods
Comment Utility
100 occurrences doesn't take long to convert - feel free to post them.
0
 

Author Comment

by:sabecs
Comment Utility
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]);
0
 
LVL 35

Accepted Solution

by:
Terry Woods earned 400 total points
Comment Utility
$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.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now