runnerjp Posted January 7, 2013 Share Posted January 7, 2013 Hello, I have a text file with a list of post codes (one on each line) I am trying to read it to see if the postcode is in the list: <?php $file = fopen("postcodes.txt", "r"); $i = 0; while (!feof($file)) { $line_of_text .= fgets($file); } $members = explode("\n", $line_of_text); fclose($file); if (in_array("SE5", $members)) { echo "Got you"; } ?> Have I gone wrong somewhere as it does not show Got you even though when I use print_r($members); it shows: Array ( [0] => SE1 [1] => SE2 [2] => SE3 [3] => SE4 [4] => SE5 [5] => SE6 [6] => SE7 [7] => SE8 [8] => SE9 [9] => SE10 [10] => SE11 [11] => SE12 [12] => SE13 [13] => SE14 [14] => SE15 [15] => SE16 [16] => SE17 [17] => SE18 [18] => SE19 [19] => SE20 [20] => SE21 [21] => SE22 [22] => SE23 [23] => SE24 [24] => SE25 [25] => SE26 [26] => SE27 [27] => SE28 ) Quote Link to comment Share on other sites More sharing options...
scootstah Posted January 7, 2013 Share Posted January 7, 2013 I copied your code and it works fine for me. If you are using Windows, your issue is probably that Windows uses "\r\n" line endings while Unix uses "\n". I am using Linux, so "\n" works for me. However, you should be populating the array with each line instead of exploding a string. <?php $file = fopen("postcodes.txt", "r"); $members = array(); while (!feof($file)) { $members[] = fgets($file); } fclose($file); if (in_array("SE5", $members)) { echo "Got you"; } Quote Link to comment Share on other sites More sharing options...
cpd Posted January 7, 2013 Share Posted January 7, 2013 (edited) // Create an array to house the postcodes $postcodes = array(); // Open the file and cycle through stripping any \n characters from each postcode. $f = fopen("file.txt", "r"); while(!feof($f)) $postcodes[] = trim(fgets($f)); fclose($f); // Is SE5 in the array? if(in_array("SE5", $postcodes)) echo "Found you"; I didn't like your logic so re-wrote it. There's no reason why that shouldn't work. Edit: Sorry scootstah, didn't see your post. Edited January 7, 2013 by cpd Quote Link to comment Share on other sites More sharing options...
Christian F. Posted January 7, 2013 Share Posted January 7, 2013 Why not just use file () to get the contents of the file? It'll return return an array with every line in its own index, just as you want it. No need for the whole fopen (), fgets () and fclose () dance. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted January 7, 2013 Share Posted January 7, 2013 You probably have other things before or after the data, such as tabs, spaces, or a \r along with the \n. Try the following - <?php $members = file("postcodes.txt"); // get all the lines at once $members = array_map('trim',$members); // trim everything if(in_array("SE5", $members)){ echo "Got you"; } Quote Link to comment Share on other sites More sharing options...
cpd Posted January 7, 2013 Share Posted January 7, 2013 Why not just use file () to get the contents of the file? It'll return return an array with every line in its own index, just as you want it. No need for the whole fopen (), fgets () and fclose () dance. Because we're not that smart Quote Link to comment Share on other sites More sharing options...
runnerjp Posted January 7, 2013 Author Share Posted January 7, 2013 Ah yes that works great. Sorry it been a while since i have coded and it 100% not like riding a bike Quote Link to comment Share on other sites More sharing options...
runnerjp Posted January 7, 2013 Author Share Posted January 7, 2013 Sorry just a quick question. Would the text file need to be like this: 1 2 3 4 5 6 what if the text file is 1,2,3,4,5,6,7 ??? Quote Link to comment Share on other sites More sharing options...
Christian F. Posted January 7, 2013 Share Posted January 7, 2013 Then you use file_get_contents () and explode () the resulting string. That said, the former was the expected format in the code you gave us in your OP. Which is why we all build upon that. That's why it's so important to be specific, and post examples of the actual data (pattern), when asking for help. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.