Rifts Posted March 24, 2012 Share Posted March 24, 2012 Hey guys, I have this super simple code which will not work and I'm very confused as to why. Here is the code // read the file into an array called $users $users= file('users.txt'); if (in_array("joe", $users)) { echo "Got Joe"; } When I print_r($users) everyone looks fine like this but it never returns true, Why? Array ( [0] => joe [1] => bob [2] => mike [3] => ted [4] => fred ) Link to comment https://forums.phpfreaks.com/topic/259649-cant-check-in_array-from-file/ Share on other sites More sharing options...
QuickOldCar Posted March 24, 2012 Share Posted March 24, 2012 change: $users= file('users.txt'); to: $users= array_map('trim',file('users.txt')); There is white space,new lines and the in_array() is searching for an exact match Link to comment https://forums.phpfreaks.com/topic/259649-cant-check-in_array-from-file/#findComment-1330797 Share on other sites More sharing options...
Rifts Posted March 24, 2012 Author Share Posted March 24, 2012 thank you! Link to comment https://forums.phpfreaks.com/topic/259649-cant-check-in_array-from-file/#findComment-1330801 Share on other sites More sharing options...
QuickOldCar Posted March 24, 2012 Share Posted March 24, 2012 Something a little extra. <?php $username = 'joe'; $username = ucwords(strtolower(trim($username))); $filename = 'users.txt'; if(file_exists($filename)){ $users= array_map('trim',file($filename)); if(in_array(strtolower($username), array_map('strtolower', $users))){ echo "Got $username"; } else { echo "$username not found"; } } else { echo "$filename doesn't exist."; } ?> Link to comment https://forums.phpfreaks.com/topic/259649-cant-check-in_array-from-file/#findComment-1330804 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.