Jump to content

Accessing a text file


pmorrison

Recommended Posts

Hi,

I am trying to access a text file which contains a list of postcodes, each on a separate line. I want to go through this file, checking to see if the postcode entered is valid. I tried using it, but realised that it wasnt searching the file correctly. I have put in the line echo "$numLines"; to see how many lines it says are in the text file, it says there is only 1, but I know there are more. Am I trying to access the file incorrectly? I have put the list.txt file in the same directory as the php file.

Cheers,

Paul

$filename = "/list.txt"; // File which holds all data
$inputString = $postcode;
$arrFp = file( $filename ); // Open the data file as an array
$numLines = count( $arrFp ); // Count the elements in the array

$numWords = count( $arrWords ); // Count the words in the string

for($j=0; $j<$numLines; $j++) {
echo "$numLines";
$outputstring = "not found";
if(strstr($inputString, trim( $arrFp[$j] ))) {
$errorlist = errorlist('Please fill in the "Postcode" field with a valid postcode', $errorlist);
$errorform = 1;
$postcodestyle = ' ; color:red';
$outputstring = "found it";
}
echo $outputString; // Echo the string replacing restricted words
} }
Link to comment
Share on other sites

Hi Ray,

I tried it without the / but it then doesnt load the page when I click on the submit button, I get a blank page with just the header. However, when I comment out the $arrFp = file( $filename ); line it loads Ok, but obviously doesnt look at the file.

Paul
Link to comment
Share on other sites

I have been looking into using text files with PHP and I am wondering if I need some kind of delimiter to show PHP that it is a new line. Currently each postcode is on a separate line, but just separated using a usual return, no \n or any other delimiter. Could this be a problem?

Paul
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.