Fix `ereg is deprecated` errors in PHP 5.3

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.

To migrate ereg():

ereg('\.([^\.]*$)', $this->file_src_name, $extension);

becomes

preg_match('/\.([^\.]*$)/', $this->file_src_name, $extension);

Notice that I wrapped the pattern (\.([^\.]*$)) around / /, which are RegExp delimiters. If you find yourself escaping / too much (for an URL for example), you might want to use the # delimiter instead.

To migrate ereg_replace():

$this->file_dst_name_body = ereg_replace('[^A-Za-z0-9_]', '', $this->file_dst_name_body);

becomes

$this->file_dst_name_body = preg_replace('/[^A-Za-z0-9_]/', '', $this->file_dst_name_body);

Again, I just added delimiters to the pattern.
If you are using eregi functions (which are the case-insensitive version of ereg), you’ll notice there’re no equivalent pregi functions. This is because this functionality is handled by RegExp modifiers.

Basically, to make the pattern match characters in a case-insensitive way, append i after the delimiter:

eregi('\.([^\.]*$)', $this->file_src_name, $extension);

becomes

preg_match('/\.([^\.]*$)/i', $this->file_src_name, $extension);
  1. Not working your eregi(). It is your false statement. Don’t write this in future. OKAY ?

  2. Deprecated: Function eregi() is deprecated in C:\wamp\www\memories-on-gold\includes\classes\language.php on line 87 any one help me.

  3. Nice topic . Thank you for posting it !

  4. Thanx alot its very helpful

  5. nekomata 23 days ago

    Thanks for this page. It is really clear.

  6. We had a similar issue, but our ereg was comparing the contents of a variable to something else - ereg($AllowedTypes,getExt($sFileName). I found a workaround that may help alot of people here…

    What preg_match (the replacement for ereg) is looking for is delimiters around the value. THis works great, unless you have a variable that’s used elsewhere and changing the value won’t work.

    What I did was to write the line as such:

    preg_match(”#”.$AllowedTypes.”#”,getExt($sFileName)

    effectively enclosing the value in #s as the delimiter. BTW - the ‘/’ is not the required delmititer, it can be just about anything, as long as it’s the same on both ends. Adding the # front and back did the trick. A hack? Yeah, probably, but it does work. Saved my ass, for sure.

    • Guillermo Rauch 25 days ago

      FTA: “Notice that I wrapped the pattern (\.([^\.]*$)) around / /, which are RegExp delimiters. If you find yourself escaping / too much (for an URL for example), you might want to use the # delimiter instead.”

  7. I’m having troubles using preg_match() on a constant.
    How to convert this old line? BASEURL is a constant.

    eregi(BASEURL, $_SERVER['HTTP_REFERER'])

  8. Installed osC 2.2 on web server and went fine. To develop locally, I installed XAMPP on local computer, then installed osC 2.2 locally and got this error. Great post, easy to follow, fixed my problem!

  9. I have this problem

    Deprecated: Function ereg() is deprecated in C:\xampp\htdocs\member.php on line 109

    if (!ereg(”MSIE”,$_SERVER['HTTP_USER_AGENT']))

    How can i fix this?

  10. I have this problem:

    Function eregi() is deprecated in C:\xampp\htdocs\catalog\admin\file_manager.php on line 29

    if (!eregi(’^’ . DIR_FS_DOCUMENT_ROOT, $current_path)) $current_path = DIR_FS_DOCUMENT_ROOT;

    How can i fix this?

    Thanks

  11. Set the error reporting to E_ALL ^E_DEPRECATED where it is currently being set to E_ALL

    change error_reporting(E_ALL); to
    error_reporting(E_ALL ^E_DEPRECATED);

    and everything will be perfect.

  12. ahmed morsy about 1 month ago

    hi
    i have appserv 2.6.0 and i setup oscommerce 2.2rc and i recive this error

    Fatal error: Call to undefined function get_magic_quotes_gpc() in C:\AppServ\www\catalog\admin\includes\functions\compatibility.php on line 46

  13. Hi!

    I use sql_regcase, that is is deprecated.

    Did you know an sql_regcase alternative ?

    Example (to remove sql syntax):
    $string = preg_replace(sql_regcase(”/(from|select|insert|delete|where|drop table|show tables|#|\*|–|\\\\)/”),”",$string);

    Thank you!

    • Sorry for the newbie question…

      This is the answer:

      $string = preg_replace(”/(from|select|insert|delete|where|drop table|show tables|#|\*|–|\\\\)/i”,”",$string);

      This is because ’sql_regcase’ just make the regular expression for case insensitive match, so… just put ‘i’ in the last ‘/’.

      Simple… but, why I don’t saw this before… :)

      Thank you very much!

  14. Hvala na postu!
    Thank You for this post!
    Very elegant solution!

  15. Thanks for this post it’s just exactly what i was looking for

  16. Thaaaaaaaaaaanks its working !

  17. Thanks for this, fixed my problem after we upgraded PHP to 5.3 and our Wordpress theme choked. Much appreciated!

  18. Thanks, ymmd :)

  19. so, what would i edit to make this work properly:

    elseif(!eregi(”http://”,$uri) && !eregi(”https://”,$uri))

    anybody? im really stuck! thanks in advance

  20. A very usefull post, it saved my day. Thnx

  21. Thanks for this! It helped me writing a Perl script that find all these deprecated functions.
    You can download the script here:
    http://nthinking.net/index.html#reDeprec.html

  22. Great site. helps me a lot.

  23. Hi.. ereg() with preg_match() is working well

  24. what about this

    if (isset($_SERVER['HTTP_HOST']) AND isset($_SERVER['HTTP_REFERER'])) {
    $visitorInfo['referer'] = (!eregi($_SERVER['HTTP_HOST'],$_SERVER['HTTP_REFERER'])) ? $_SERVER['HTTP_REFERER'] : “” ;
    } else {
    $visitorInfo['referer'] = “”;
    }

  25. Hi, thanks for your writing. I fixed almost all deprecations as your guide, but except this code:

    if (preg_match(’^’.$arrParam[0].’=', $value))

    When I put / / as delimiter before and after ^ to become:

    if (preg_match(’/^/’.$arrParam[0].’=', $value))

    I keep getting error as:

    Unknown modifier ‘r’ in C:/Program Files/EasyPHP5.3.0/www/taskfreak/include/classes/tzn_generic.php

    Note: tzn_generic.php is the file I put above code.

    Please help me.

    Many thanks,
    Seri

  26. Hi.

    Can you please tell me what should I change in the code below to make things work:
    $check_url = str_replace(”\”", “”, $check_url);
    if ((eregi(”]*script*\”?[^>]*>”, $check_url)) || (eregi(”]*object*\”?[^>]*>”, $check_url)) ||
    (eregi(”]*iframe*\”?[^>]*>”, $check_url)) || (eregi(”]*applet*\”?[^>]*>”, $check_url)) ||
    (eregi(”]*meta*\”?[^>]*>”, $check_url)) || (eregi(”]*style*\”?[^>]*>”, $check_url)) ||
    (eregi(”]*form*\”?[^>]*>”, $check_url)) || (eregi(”\([^>]*\”?[^)]*\)”, $check_url)) ||
    (eregi(”\”", $check_url))) {

    I got this error:
    Deprecated: Function eregi() is deprecated in E:\xampp\htdocs\laculprietenia\maincore.php on line 35

    Deprecated: Function eregi() is deprecated in E:\xampp\htdocs\laculprietenia\maincore.php on line 35

    Deprecated: Function eregi() is deprecated in E:\xampp\htdocs\laculprietenia\maincore.php on line 36

    Deprecated: Function eregi() is deprecated in E:\xampp\htdocs\laculprietenia\maincore.php on line 36

    Deprecated: Function eregi() is deprecated in E:\xampp\htdocs\laculprietenia\maincore.php on line 37

    Deprecated: Function eregi() is deprecated in E:\xampp\htdocs\laculprietenia\maincore.php on line 37

    Deprecated: Function eregi() is deprecated in E:\xampp\htdocs\laculprietenia\maincore.php on line 38

    Deprecated: Function eregi() is deprecated in E:\xampp\htdocs\laculprietenia\maincore.php on line 38

    Deprecated: Function eregi() is deprecated in E:\xampp\htdocs\laculprietenia\maincore.php on line 39

    Thank you.

  27. Can I ask a really stupid question……what file am i supposed to edit? i cant find that code in the php.ini file. Am I being stupid??!!

    • nibuster 4 months ago

      doesen’t browser informs you about error when you test your project? it should say where error is. file and line no.

  28. nibuster 4 months ago

    can’t resolve this one

    $msg =ereg_replace(”(http|https|ftp|gopher|news)://([$ACCEPT_CHARS]+)”, “\\1://\\2” , $msg);

  29. Thank you!

  30. nibuster 4 months ago

    thx!

  31. Thanks alot its working. also i have a problem with split

    Function split() is deprecated if you have any idea can u share with us

  32. sudhakar 6 months ago

    A lot of thanks!!

  33. It’s worth noting that whilst ereg returns FALSE when there is no match, preg_match returns 0.

  34. Jonathan 6 months ago

    Thank you very much for this clear and simple explanation ! Much appreciated.

  35. Christian Alexis 6 months ago

    Thanks!! Really!! I Luv U!!!

  36. Prashanth 6 months ago

    Hi thank u very much, it helped me a lot………

  37. Clear Fix For NeverEnding Programmings !

    What’s happen with split function deprecated too ?
    veru simple : split() change for explode() in same conditions

  38. Thanks, excellent post. I would have expected this to show up on the php doc page for eregi, I guess they want you to figure it out yourself!

  39. Many thanks for that, seems to have fixed 80% of my problems - only ones it doesn’t seem to fif is where ereg(’^’ ) occurs replacing as you say produces a: Warning: preg_match() [function.preg-match]: Unknown modifier ‘a’ in…

    But that’s in admin - I can live with that for a while as long as the front end work ;) Thank you

  40. Thanks!

  41. Thanks worked well

  42. GREAT!GREAT!GREAT!

  43. Many thanks for this! I was up late doing a server migration, figured I’d upgrade to PHP 5.3… and of course I break one of my online stores, http://ambermariebently.com. Your fix saved me at 2am, I owe you big time I think:)

  44. I’ve found that almost anything can be used as a delimiter with preg_match. I personally use ~ as it’s the least used character in my regex patterns.

  45. I’ve found that almost anything can be used as a delimiter with preg_match. I personally use ~ as I use it the least.