LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   PHP Pull File Lines Into Table (http://www.linuxquestions.org/questions/programming-9/php-pull-file-lines-into-table-452155/)

windisch 06-06-2006 01: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:

<?php
$filename 
"MythMusic.alw";
$handle fopen($filename"r");
if ( 
$handle )
{
$contents fread($handlefilesize($filename));
fclose($handle);
echo 
"<P2>$contents</P2>";
}
else
{
die (
"<H1>No Request Songs In Queue</H1>");
}
?>


graemef 06-06-2006 01: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 01:44 PM

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

PeO 11-01-2006 03: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 05:26 PM

Try the runtime option auto_detect_line_endings
PHP Code:

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


PeO 11-02-2006 01:26 PM

Thx it worked very well !!:p

PeO 11-02-2006 04: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 05:06 PM

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


All times are GMT -5. The time now is 05:01 AM.