Skip to content
PHP Logo

What is the difference between ereg() and preg_match() in PHP, and when should each be used?

The main difference between ereg() and preg_match() in PHP is that ereg() performs a case-insensitive regular expression match, while preg_match() allows for more advanced regular expressions and provides better performance. ereg() is considered deprecated as of PHP 5.3.0 and should be avoided in favor of preg_match().

php
// Using preg_match() to perform a regular expression match
$string = "Hello, World!";
if (preg_match("/hello/i", $string)) {
    echo "Match found!";
} else {
    echo "No match found.";
}