Does array element contain substring? [duplicate]
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";}
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';}