CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   PHP (http://www.codingforums.com/forumdisplay.php?f=6)
-   -   Going through a txt file (http://www.codingforums.com/showthread.php?t=1286)

avelonx 07-03-2002 05:50 AM

Going through a txt file
 
Could someone please show me a small example of how to go through every line of a txt file and to show the information for each line. Alls I need is the code to go through the txt file

Flamerule 07-03-2002 08:18 AM

The best way to show line per line the contents of a file is using the file(); function which returns an array where each element of this array contains a line of the file.

lines.txt :
Quote:

Line 1
Line 2
Line 3
Line 4
PHP Script :
PHP Code:

$lines file("lines.txt");
foreach(
$lines as $data){
        echo 
$data "<br>";
}
// You can also directly input the text file in the foreach function :
foreach(file("lines.txt") as $data)
        echo 
$data ''<br>"; 

The result would look exaclty like the text file.
You can also use int readfile(string file); to read and print out the whole content of the file which is better is you just want to write out the whole textfile rather than doing line per line manipulation. (It returns the number of characters read.)

avelonx 07-03-2002 03:31 PM

Thank you


All times are GMT +1. The time now is 02:30 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.