How does the preg_match function differ from the ereg function in terms of validating email addresses in PHP?

The preg_match function is more commonly used in modern PHP versions for pattern matching and validation compared to the ereg function. When validating email addresses, preg_match offers better performance and flexibility due to its support for regular expressions. To validate an email address using preg_match, you can use a regular expression pattern that matches the typical structure of an email address.

$email = "example@example.com";

if (preg_match('/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/', $email)) {
    echo "Valid email address";
} else {
    echo "Invalid email address";
}