Replace ereg with preg_match

How to update to using preg_match on newer versions of PHP

I was recently (if you are reading this in 2025, assume recently was about 14 or 15 years ago!) developing on a newer version of PHP than what is currently used on most of our servers and I was getting a warning telling me that the function ereg was depreciated.

A little searching told me that is is going to be totally removed from PHP 6 so in the interest of keeping my code nice and up to date I made the following changes;

Use preg_match instead of ereg and wrap the expression with //

Here is an example;

ereg("^[-]?[0-9]+([\.][0-9]+)?$", $var);

Becomes

preg_match("/^[-]?[0-9]+([\.][0-9]+)?$/", $var);

Recent posts View all

Web Dev

Check in with your database

It pays to take a step back and look at how your database is set up every so often. You will often find quick wins and hidden tech debt.

Web Dev Productivity

Keeping on top of website updates

Learn what website updates are, what they entail, why they are important, and how we can help