Solved

PHP error - Deprecated: Function ereg_replace() is deprecated

Posted on 2012-03-21
8
521 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
ID: 37750519
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 109

Expert Comment

by:Ray Paseur
ID: 37750537
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 109

Expert Comment

by:Ray Paseur
ID: 37750548
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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

Author Comment

by:sabecs
ID: 37750831
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
 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 100 total points
ID: 37750863
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
ID: 37750877
100 occurrences doesn't take long to convert - feel free to post them.
0
 

Author Comment

by:sabecs
ID: 37750905
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
ID: 37750919
$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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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 dynamically set the form action using jQuery.

830 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