我正在开发基于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
在禁止发件人列表中。
我该如何解决这个问题?
也许你正在寻找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
}
另一个
带有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)