MyFault MyFault - 5 years ago 302
PHP Question

PHP: Check if string is part of an array

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?

Answer Source

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
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download