Php validates email address based on domain name

I need to filter out some email address based on the domain name: Basically, if the domain name is yahoo-inc.com, facebook.com, baboo.com .. (and several others), the function should do something, and if the domain different, he has to do something else. The only way I know this is to use a pattern / regex with preg_match_all and create cases / conditions for each banned domain (for example, if domain = yahoo-inc) do it elseif (domain == facebook.com) do it .. etc., but I need to know if there is a simpler / concis way to include all domains that I want to filter in a single variable / array and then apply only 2 conditions (for example, if the email is blacklisted { do something else} else {do ​​something else}

+1
source share
3 answers

Extract the part of the domain (that is, everything after the last "@"), enter it, and then use in_array to check if it is on your blacklist:

 $blacklist = array('yahoo-inc.com', 'facebook.com', ...); if (in_array($domain, $blacklist)) { // bad domain } else { // good domain } 
+2
source

Adding to @Alnitak is the complete code you need

 $domain = explode("@", $emailAddress); $domain = $domain[(count($domain)-1)]; $blacklist = array('yahoo-inc.com', 'facebook.com', ...); if (in_array($domain, $blacklist)) { // bad domain } else { // good domain } 
+2
source

Well, here is a very simple way to do this, the VALID-email address should contain only one @ character, so as a test you can just blow up the string at @ and collect the second segment.

Example:

 if (filter_var($user_email, FILTER_VALIDATE_EMAIL)) { //Valid Email: $parts = explode("@",$user_email); /* * You may want to use in_array if you already have a compiled array * The switch statement is mainly used to show visually the check. */ switch(strtolower($parts[1])) { case 'facebook.com': case 'gmail.com': case 'googlemail.com': //Do Something break; default: //Do something else break; } } 
+1
source

All Articles