我的代码:
<?php
$pass = "12345";
//checkPass($pass, $user, $length);
$file = file_get_contents("common.txt");
$array = explode("\n", $file);
if(in_array($pass, $array) == true) {
echo "it's in the array";
}
?>
数组的前几行(我使用print_r($ array)...):
Array ( [0] => 12345 [1] => abc123 [2] => password [3] => computer [4] => 123456 [5] => tigger [6] => 1234 [7] => a1b2c3 [8] => qwerty [9] => 123 [10] => xxx [11] => money [12] => test [13] => carmen [14] => mickey [15] => secret [16] => summer [17] => internet [18] => service [19] => canada [20] => hello [21] => ranger [22] => shadow [23] => baseball [24] => donald [25] => harley [26] => hockey [27] => letmein [28] => maggie [29] => mike [30] => mustang [31] => snoopy
答案 0 :(得分:24)
如果您的文件使用Windows换行符(行以\r\n
结尾),则会在每个字符串的末尾显示一个不可见的\r
字符。通过在其中一个上运行strlen()来测试它:
echo $array[0] . ': ' . strlen($array[0]) . ' chars';
如果你得到像
这样的东西12345: 6 chars
你知道这就是问题所在!使用带有array_map()
的trim()
爆炸数组后,您可以删除这些字符:
$array = array_map('trim', $array);
答案 1 :(得分:2)
以下是我提出的有效方法:
<?php
$file = file_get_contents("common.txt");
$array = explode("\n", $file);
$pass = "snoopy";
if(in_array($pass, $array) == true) {
echo "it's in the array";
}else {
echo "it's not";
}
?>
答案 2 :(得分:2)
您也可能想要使用trim
。可能是你看不见的隐形角色。
答案 3 :(得分:1)
是$传递值0 - 31?