Page 1 of 1

how to separate and read data from text file

Posted: Tue Dec 14, 2004 12:04 am
by robbydweb
I just started learning php and cant figure this out.
If i have a text file like this:

name1|location1|message1
name2|location2|message2
ect...

How can I read 1 line at a time, separate each section and display each value ? I know how to read a whole text file, but dont know
how to break it up and display each part separaetly.

Posted: Tue Dec 14, 2004 12:18 am
by kettle_drum
Well if you use fgets() it will just read until the line break, or you can use file() to read the whole file at once, and it places each line in an array item. Then use explode() to get each part seperate.

Code: Select all

$file = "myfile.txt";
$data = file($file);
foreach($data as $line){
   $parts = explode("|", $line);
   print_r($parts);
}