View Full Version : reading a .txt file
habib
10-07-2002, 10:24 AM
Hi ALL!
I have a text file (cities.txt) which contains hundreds of cities. Is there anyway of being able to read the file and store the text in a variable?
Any working examples that anyone can point out would be greatly appreciated.
Thanks
HR
assuming you want an array variable, you would open the file and and either read the lot into a var and explode that or format your reading trigger to compile the array while reading.
Personally I like fgetcsv() for reading from files (only really useful for character-seperated lists with linebreaks tho).
$my_file = '../textfiles/cities.txt';
$rd = fopen($my_file,"r");
while (!feof ($rd)) {
$my_city[] = fgets($rd, 4096);
}
fclose($rd);
that would take each line of the file and attribute it to an indexed array $my_city - eg
$my_city[0] = "Aberdeen";
$my_city[1] = "London";
assuming each city is on a different line in the text file.
sort($my_city); would alphabetically reorganise the output.
If your txt file syntax is different and you get problems trying to run a code, just explain your syntax and ask further.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.