我正在开发基于PHP的小票务系统。
现在我想排除发送者被处理。
这是一份可能被排除在外的发件人名单:
Array (
"badboy@example.com",
"example.org",
"spam@spamming.org"
)好的-现在我想检查邮件的发件人是否与其中之一匹配:
$sender = "badboy@example.com";我认为这很容易,我想我可以用in_array()来解决这个问题。
但那又如何
$sender = "me@example.org";example.org是在数组中定义的,而不是me@example.org --但是me@example.org也应该排除,因为example.org在禁止发送者列表中。
我怎么能解决这个问题?
发布于 2016-02-16 14:34:34
也许您正在寻找stripos函数。
<?php
if (!disallowedEmail($sender)) { // Check if email is disallowed
// Do your stuff
}
function disallowedEmail($email) {
$disallowedEmails = array (
"badboy@example.com",
"example.org",
"spam@spamming.org"
)
foreach($disallowedEmails as $disallowed){
if ( stripos($email, $disallowed) !== false)
return true;
}
return false
}发布于 2016-02-16 15:16:31
stripos、implode和explode函数的另一个简短的替代方案:
$excluded = array(
"badboy@example.com",
"example.org",
"spam@spamming.org"
);
$str = implode(",", $excluded); // compounding string with excluded emails
$sender = "www@example.com";
//$sender = "me@example.org";
$domainPart = explode("@",$sender)[1]; // extracting domain part from a sender email
$isAllowed = stripos($str, $sender) === false && stripos($str, $domainPart) === false;
var_dump($isAllowed); // output: bool(false)https://stackoverflow.com/questions/35435168
复制相似问题