How to Fix `ereg is deprecated` errors in PHP 5.3

If you upgraded to PHP 5.3 like the famous oscommerce 2.2, 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.

Replacing 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.

Replacing ereg_replace():

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

becomes

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);

Replacing split():

list($lat,$lng) = split(',', $coordinates);

becomes

list($lat,$lng) = preg_split('/,/',$coordinates);

you can also use explode() instead preg_list which will be available on PHP 6 :

list($lat,$lng) = explode(',',$coordinates);

Cheers,

extradrmtech

Since 20 years I work on Database Architecture and data migration protocols. I am also a consultant in Web content management solutions. I am an experienced web-developer with over 10 years developing PHP/MySQL, C#, VB.Net applications ranging from simple web sites to extensive web-based business applications. When not writing code, I like to dance salsa and swing and have fun with my little family.

You may also like...

Leave a Reply