What are some alternative approaches to wildcard matching for email domain checks in PHP?
When checking email domains in PHP, wildcard matching can be useful to allow for flexibility in domain verification. One approach to wildcard matching is to use regular expressions to match patterns in the email domain. This can be done by replacing the wildcard character (*) with a regex pattern that matches any sequence of characters. Another approach is to manually parse the email domain and compare it against a list of allowed domains, taking into account wildcard entries.
// Using regular expressions for wildcard matching in email domain checks
$email = "example@gmail.com";
$allowed_domains = ["*.com", "*.net"];
foreach ($allowed_domains as $domain) {
$regex = str_replace("*", ".*", $domain);
if (preg_match("/$regex$/", substr(strrchr($email, "@"), 1))) {
echo "Email domain is allowed: $email";
break;
}
}