tags:

views:

82

answers:

4

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
+1  A: 

is $pass a value of 0 - 31?

chuck
$pass = "12345";
Andrew
+2  A: 

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";    
}

?>
Jeremy Morgan
+2  A: 

you may want to use trim on that too. could be invisible chars you are not seeing by eye.

chuck
+6  A: 

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);
yjerem