"Deprecated: Function ereg_replace() is deprecated in" Php 5.3


 I have following line was working well in php 5.2 but not in 5.3. Is it possible to fix it?

$q = ereg_replace("S", "s", $q);

Open in new window

Who is Participating?
Beverley PortlockCommented:
One point that has not been mentioned is this - just because a function is deprecated does not mean that it does not work. ereg_replace is fine in version 5.3

Deprecated means it will be removed in a FUTURE version so you should not write any new software using it, but it will still work in the current version. You can suppress the error by changing settings in php.ini.
Try using preg_match.
Marco GasiFreelancerCommented:
You have to use preg_replace instead.

$q = preg_replace("S", "s", $q);
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

In your case you can use str_replace instead.
Marco GasiFreelancerCommented:
Here you find info about preg_replace function: http://it.php.net/manual/en/function.preg-replace.php

"If you upgraded to PHP 5.3, chances are high you’re going to run into a few warnings or deprecated function messages.
An example is the ereg family of functions, which are gone for good, as they were slower and felt less familiar than the alternative Perl-compatible preg family."


Hope this helps you
Marco GasiFreelancerCommented:
Oh, yeah, pius_babbun is rigth: I forgot to mention to add delimiters: $q = preg_replace("/S/", "s", $q);

I'm sorry. :-)
Ray PaseurCommented:
You can use error_reporting(E_ALL ^ E_DEPRECATED) to suppress the auto-annoyance messages.  However you really want to do a code scan and remove the ereg() in favor of the corresponding preg() functions.  And as a practical matter you do not need any regular expression to replace one character with another.  Instead of REGEX you can use this to mung the contents of $q:

$q = str_replace("S", "s", $q);
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.