Does array contain (part of) string?
This question already has an answer here:
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!
foreach ($forbiddennames as $forbiddenname) {
$nametocheck = strtolower($stringtocheck);
if(strpos($stringtocheck, $forbiddenname) !== false) {
echo "This is a forbidden username.";
break;
}
}
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';
}
链接地址: http://www.djcxy.com/p/13120.html
上一篇: PHP如果不在字符串中
下一篇: 数组是否包含(的一部分)字符串?