php根据域名验证电子邮件地址 [英] php validate email address based on the domain name
问题描述
我需要根据域名过滤一些电子邮件地址:
基本上,如果域名是yahoo-inc.com,facebook.com,baboo.com ..(以及其他一些),该功能应该做一些事情,如果域名不同,它应该做其他事情。
我知道这样做的唯一方法是使用带有preg_match_all的模式/正则表达式,并为每个balcklisted域创建案例/条件(例如,如果domain = yahoo-inc)执行此elseif(domain == facebook.com)这样做......等我需要知道是否有更简单/更简洁的方法来包含我想要在单个变量/数组中过滤的所有域,然后只应用2个条件(例如,如果电子邮件是黑名单{do something} else {做其他事情}
I need to filter some email address based on the domain name : Basically if the domain name is yahoo-inc.com, facebook.com, baboo.com .. (and a few others) the function should do something and if the domain is different it should do something else . The only way I know to do this is to use a pattern/regex with preg_match_all and create cases/conditions for each balcklisted domain (e.g if domain = yahoo-inc) do this elseif (domain == facebook.com ) do this ... etc but I need to know if there is a more simple/concis way to include all the domains that I want to filter in a single variable/array and then apply only 2 conditions (e.g if email is in black list {do something } else {do something else}
推荐答案
提取域名部分(即最后一个'@'后的所有内容),关闭它,然后使用 in_array
检查它是否在你的黑名单中:
Extract the domain portion (i.e. everything after the last '@'), down case it, and then use in_array
to check whether it's in your blacklist:
$blacklist = array('yahoo-inc.com', 'facebook.com', ...);
if (in_array($domain, $blacklist)) {
// bad domain
} else {
// good domain
}
这篇关于php根据域名验证电子邮件地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!