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
https://forums.phpfreaks.com/topic/4239-accessing-a-text-file/
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
https://forums.phpfreaks.com/topic/4239-accessing-a-text-file/#findComment-15157
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.