簡體   English   中英

PHP:檢查字符串是否為數組的一部分

[英]PHP: Check if string is part of an array

我正在基於PHP的小型票務系統上工作。

現在,我想排除發件人的處理。

這是排除的發件人的可能列表:

Array ( 
"badboy@example.com",
"example.org",
"spam@spamming.org"
)

好的-現在,我想檢查郵件的發件人是否與以下其中之一匹配:

$sender = "badboy@example.com";

我認為這很容易,我想可以使用in_array()解決此問題。

但是關於

$sender = "me@example.org";

在數組中定義了example.org ,但me@example.orgme@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
}

與另一短替代striposimplodeexplode功能:

$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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM