dongmaopan5738 2016-02-16 14:28
浏览 89
已采纳

PHP:检查字符串是否是数组的一部分

I'm working on my little ticketing-system based on PHP.

Now I would like to exclude senders from being processed.

This is a possible list of excluded senders:

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

Okay - now I would like to check if the sender of an mail matches one of these:

$sender = "badboy@example.com";

I think this is quite easy, I think I could solve this with in_array().

But what about

$sender = "me@example.org";

example.org is defined in the array, but not me@example.org - but me@example.org should also excluded, because example.org is in the forbidden-senders-list.

How could I solve this?

  • 写回答

2条回答 默认 最新

  • dongqindan4406 2016-02-16 14:34
    关注

    Maybe you are looking for stripos function.

    <?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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Pwm双极模式H桥驱动控制电机
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题