19

I'd like a function that checks whether an array's items contain a string. As such:

array(1 => 'Super-user', 'Root', 'Admin', 'Administrator', 'System', 'Website', 'Owner', 'Manager', 'Founder');

And then checking for admin12 should return true as a part of admin12 (admin) is also a part of the array.

I came this far:

$forbiddennames= array(1 => 'Super-user', 'Root', 'Admin', 'Administrator', 'System', 'Website', 'Owner', 'Manager', 'Founder');    

if(in_array( strtolower($stringtocheck), array_map('strtolower', $forbiddennames))){
        echo '"This is a forbidden username."';
    } else {
        echo 'true';
    }
}

Only this only echos "This is a forbidden username." when I check for admin. I want it also to echo when checking for admin12.

Is this possible (and how)?

CragMonkey
  • 808
  • 1
  • 11
  • 22
Isaiah
  • 1,852
  • 4
  • 23
  • 48
  • 4
    I don't think this is much of a duplicate with http://stackoverflow.com/questions/4366730/how-to-check-if-a-string-contains-specific-words - an array of string definitely has different qualities than just a string, and I'm looking into doing this with a regexp. How can one object to this "marked as duplicate"? – the Dec 28 '13 at 15:21

4 Answers4

8

Loop through the $forbiddennames array and use stripos to check if the given input string matches any of the items in the array:

function is_forbidden($forbiddennames, $stringtocheck) 
{
    foreach ($forbiddennames as $name) {
        if (stripos($stringtocheck, $name) !== FALSE) {
            return true;
        }
    }
}

And use it like below:

if(is_forbidden($forbiddennames, $stringtocheck)) {
    echo "This is a forbidden username.";
} else {
    echo "True";
}

Demo!

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
  • This allows "admin12" to echo true, while I want it to echo "this is a..." because "admin12" (input) contains "Admin" (from the f.n. array). – Isaiah Oct 27 '13 at 14:06
  • @Isaiah: That was a minor mistake from my part. I accidentally switched the parameters for `stripos()`. Please see the updated answer (and the [demo](https://eval.in/57770)) – Amal Murali Oct 27 '13 at 14:06
3
foreach ($forbiddennames as $forbiddenname) {
    $nametocheck = strtolower($stringtocheck);
    if(strpos($stringtocheck, $forbiddenname) !== false) {
        echo "This is a forbidden username.";
        break;
    }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Ash
  • 57
  • 2
2

It doesn't really matter if you use array_map, foreach or something different. Possible solution:

$forbiddenNames = array('admin', 'bannedName');
$input = 'Admin12';
$allowed = true;
foreach($forbiddenNames as $forbiddenName) {
    if(stripos($input, $forbiddenName) !== false) {
        echo $input, ' is invalid';
        $allowed = false;
        break;
    }
}
if($allowed === true) {
    echo $input, ' is valid';
}
Rangad
  • 2,110
  • 23
  • 29
2

you want PHPs 'strpos' function. loop through each array element, and then check each element against 'strpos' PHP strpos reference

foreach($forbiddennames as $fn){
 if(strpos($stringtocheck,$fn)){
  //found it!
 }else{
  //not found!
 }
}
Chris Wesson
  • 299
  • 1
  • 4