Function ereg() is deprecated possible fix
Hi all,
Just wanted to share a little code patch / fix for people encountering the “Function ereg() is deprecated” problem.
This issue arises if you’re running PHP 5.3. In this new version, PHP has decided that they want to start phasing out ereg() and so they do so by throwing out exceptions/error messages when ereg() is being executed – kinda to warn and say “dudes, we are gonna be killing this function, we are letting you use it now but we are gonna irritate you till you stop using it!
”. The ereg() function still works, but with a whole bunch of exceptions – it gets annoying unless you ignore them and there’s a possibility that if you decide to upgrade to the next level of PHP, your function executing ereg() will fail.
We were initially facing this problem with the Easy Speak Mailing List plugin for WordPress hence it was necessary that we find a fix for it because we really like the plugin and we want to use it for our Game Blog.
Upon searching online, we have a couple of fixes and I’ve identified the top 2 fixes IMHO. Here they are, summarised for your easy reference. (Mini Disclaimer – I am NOT the authority in this area, I simply found the possible solutions in Google for our problem and just wanted to share it with you guys. Please patch / fix at your own discretion. Please do not flame me if something breaks.
)
If all’s great, here goes:
1) Replace ereg() with the PCRE family of functions. You can look up the matching of ereg() and PCRE family here. – More complicated but the ubiquitous recommended solution – especially since it’s mentioned in the PHP manual.
Although this is the recommended solution, you need to really sit down and plough through the syntax as you can’t simply just ctrl+H and replace all ereg() with preg_match(). Unfortunately the syntax, codes and replacements got a little bit too complicated for me, after spending 2 hours on it, I decided to give up and Google for other solutions. I managed to find #2 below and solve the problem.
2) Replace ereg() with mb_ereg()
Simply replace all ereg() functions with the mb_ereg() function. This is a very easy fix which worked for us, but I’m not quite sure about the cons although the web has commented that mb_ereg() is slower than preg_match() – which is the recommended replacement for ereg() (see point 1 above). This author actually did a speed test for mb_ereg() function versus the preg_match(). So… *Shrugs*. The replacement / fix worked for us though!
There were other solutions like suppressing the deprecated errors or changing ereg() to @@ereg(). You can read more about them here. But I don’t think they are really that efficient of a solution. Correct me if I’m wrong though.
Hopefully this little article helps you in your quest to fix up this irritating error message. Feel free to leave a comment if you have any questions or any suggestions on how we can make the fix more efficient.
Cheers!





Thanks for sharing your research!
An addition for the lazy ones, two shell 1liners for a quick replacement of all eregi and ereg calls within your php files:
find PATHTOYOURSCRIPTS -type f -name \*.php -exec perl -p -i -e ‘s/\beregi\b/mb_eregi/g;’ {} \;
find PATHTOYOURSCRIPTS -type f -name \*.php -exec perl -p -i -e ‘s/\bereg\b/mb_ereg/g;’ {} \;