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

1
  • 4
    I don't think this is much of a duplicate with stackoverflow.com/questions/4366730/… - 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"? Commented Dec 28, 2013 at 15:21

4 Answers 4

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!

Sign up to request clarification or add additional context in comments.

2 Comments

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: That was a minor mistake from my part. I accidentally switched the parameters for stripos(). Please see the updated answer (and the demo)
3
foreach ($forbiddennames as $forbiddenname) {
    $nametocheck = strtolower($stringtocheck);
    if(strpos($stringtocheck, $forbiddenname) !== false) {
        echo "This is a forbidden username.";
        break;
    }
}

2 Comments

You should check for !==, as strpos could also return 0 if a match is found
Yep, absolutely right
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';
}

1 Comment

Works as well, thanks!
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!
 }
}

4 Comments

Why do you go through the entire array, if there is the possibility to break after an invalid name is found?
'strripos' is better if you want case-insensitive search. I would use it over 'strpos', but the code sample would basically be the same
@ChrisWesson: That's different. I think you meant stripos() (with one R) :)
word. one 'r'. thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.