-   Programming (
-   -   PHP Pull File Lines Into Table (

windisch 06-06-2006 02:04 PM

PHP Pull File Lines Into Table
I have this php page that reads a file and echos out the data. I want to be able to read each line, and then perform an action upon it. What would be the best way to do that?

PHP Code:

$handle fopen($filename"r");
if ( 
$handle )
$contents fread($handlefilesize($filename));
die (
"<H1>No Request Songs In Queue</H1>");

graemef 06-06-2006 02:18 PM

Use the file() function, this reads the contents into an array one element per line. Then you can do your processing on each line.
PHP Code:

$lines file($filename); 

windisch 06-06-2006 02:44 PM

Perfect, I should be able to do what I need with the data in an array. Thanks graemef!

PeO 11-01-2006 04:14 PM

I have done it that way but it wouldn't work... :mad:

$skkfile is a textfile with lines terminated with 0x0d

$lines = explode(F, file($skkfile));
echo "0 : ".$lines[0]."<br>\n";
echo "1 : ".$lines[1]."<br>\n";
echo "2 : ".$lines[2]."<br>\n";

If I try to print the array it result:
0 : Array
1 :
2 :

Someone who can give me an hint....

keefaz 11-01-2006 06:26 PM

Try the runtime option auto_detect_line_endings
PHP Code:

$lines file($skkfile);
"0 : ".$lines[0]."<br>\n";
"1 : ".$lines[1]."<br>\n";
"2 : ".$lines[2]."<br>\n";

PeO 11-02-2006 02:26 PM

Thx it worked very well !!:p

PeO 11-02-2006 05:58 PM

I also tried to use explode to separate data inside the lines:
$table = explode('\t', $lines[$i]);

the lines in the file are data separated with a tab char (ascii 09) but I got whole line as $table[0]... :scratch:

PeO 11-02-2006 06:06 PM

Wow... found it my self....
$table = explode("\t", $lines[$i]);

All times are GMT -5. The time now is 11:49 AM.