Solved

PHP error - Deprecated: Function ereg_replace() is deprecated

Posted on 2012-03-21
8
528 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 110

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 110

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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

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 110

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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

617 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