tags:

views:

534

answers:

4

I have text file that looks like this:

1 1 1 1

1 2 3 5

4 4 5 5

I want to read this text file into array of lines and display it. Can anyone help me do this?

+2  A: 

This should get you going: php function file

Eddy
I use to use this function and I like. but it can kill you server if the file is too big since this function try to load all the file on memory, depending on the file size you need to use fopen and move the cursor
Gabriel Sosa
+1  A: 
<?php

$cont = file_get_contents("data.txt");
$lines = explode("\n",$cont); // $lines is now an array containing each line

// do something with data here

?>

make sure you use the correct line endings however as Windows uses \r\n and UNIX uses \n.

John T
A: 

You'd want to do something like this:

<?

$filename = "somefile.txt";
$arr = file($filename);
foreach($arr as $line){
    print $line . "<br />";
}

?>
Adam Plumb
+2  A: 

you can use fopen(), fgets(). see here

eg

$f=fopen("file","r");
if($f){
    while (!feof($f)) {
        $line = fgets($f, 4096);
        print $line;
    }
    fclose($f);
}
ghostdog74