Solved

PHP error - Deprecated: Function ereg_replace() is deprecated

Posted on 2012-03-21
8
520 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
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.
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 create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

773 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