How can the PHP code be modified to exclude multiple email domains without explicitly listing each one?
To exclude multiple email domains without explicitly listing each one, you can use a regular expression to check if the email domain is not in a predefined list of domains to exclude. This can be achieved by using a negative lookahead assertion in the regular expression pattern.
$email = "example@example.com";
$excluded_domains = array("domain1.com", "domain2.com");
if (!preg_match('/@(?!' . implode('|', array_map('preg_quote', $excluded_domains)) . ')/', $email)) {
// Email domain is not in the excluded list
echo "Email domain is allowed.";
} else {
// Email domain is in the excluded list
echo "Email domain is not allowed.";
}