此函数通过遍历字符数组并检查是否有任何字符与允许的字符列表不匹配来检查特殊字符。这个功能有什么问题?如果能帮上忙,非常感谢!
假设 str_split_array($stringPassed); 完美运行(我 99% 确定它确实如此,我在其他各种功能中使用它)
// returns true if a string has special characters
// $stringPassed = input string to split and check
// $checkWhiteSpace = check for whitespace, true or false
function hasSpecialCharacters($stringPassed, $checkWhiteSpace = true) {
// Array of characters ALLOWED
$allowedCharacters = array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", 1, 2, 3, 4, 5, 6, 7, 8, 9, 0);
$arrayLen = strlen($stringPassed);
$array = str_split_array($stringPassed);
for($i=0;$i<$arrayLen;$i++) {
if(!in_array($array[$i], $allowedCharacters)) {
return true;
} else if($checkWhiteSpace==true && $array[$i]==" ") {
return true;
}
}
return false;
}
再次感谢!