Jump to content

Reading from a text file


runnerjp

Recommended Posts

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 )

Link to comment
Share on other sites

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

Link to comment
Share on other sites


// 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 by cpd
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 :facepalm:

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.