我基本上需要一个函数来检查字符串的字符(每个字符)是否在数组中。
我的代码目前还不能正常工作,但无论如何,
$allowedChars = 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"," ","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"," ","0","1","2","3","4","5","6","7","8","9"," ","@",".","-","_","+"," ");
$input = "Test";
$input = str_split($input);
if (in_array($input,$allowedChars)) {echo "Yep, found.";}else {echo "Sigh, not found...";}
我希望它说“是的,找到了。”如果在 $allowedChars
中找到 $input 中的字母之一。很简单,对吧?好吧,那行不通,而且我还没有找到一个函数可以在字符串的各个字符中搜索数组中的值。
顺便说一下,我希望它只是那些数组的值,我不是在寻找花哨的 html_strip_entities 或其他任何东西,我想为允许的字符使用那个确切的数组。
最佳答案
你真的应该看看正则表达式和 preg_match 函数:http://php.net/manual/en/function.preg-match.php
但是,这应该可以满足您的特定要求:
$allowedChars = 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"," ","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"," ","0","1","2","3","4","5","6","7","8","9"," ","@",".","-","_","+"," ");
$input = "Test";
$input = str_split($input);
$message = "Sigh, not found...";
foreach($input as $letter) {
if (in_array($letter, $allowedChars)) {
$message = "Yep, found.";
break;
}
}
echo $message;
关于php - 如何检查字符串是否在数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3286411/