Reputation: 75
My code:
<?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";
}
?>
first few lines of the array (i used 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
Upvotes: 5
Views: 14390
Reputation: 176675
If your file uses Windows linebreaks (lines end in \r\n
), you'll get an invisible \r
character at the end of each of your strings. Test for it by running strlen() on one of them:
echo $array[0] . ': ' . strlen($array[0]) . ' chars';
If you get something like
12345: 6 chars
You know that's the problem! You can get rid of these characters after exploding the array using array_map()
with trim()
:
$array = array_map('trim', $array);
Upvotes: 25
Reputation: 31
you may want to use trim
on that too. could be invisible chars you are not seeing by eye.
Upvotes: 2
Reputation: 3372
Here is what I came up with that worked:
<?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";
}
?>
Upvotes: 2