LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-06-2006, 01:04 PM   #1
windisch
Member
 
Registered: Nov 2004
Location: Gahanna, Ohio, USA
Distribution: Fedora 9
Posts: 158

Rep: Reputation: 30
Question 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>");
}
?>
 
Old 06-06-2006, 01:18 PM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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); 
 
Old 06-06-2006, 01:44 PM   #3
windisch
Member
 
Registered: Nov 2004
Location: Gahanna, Ohio, USA
Distribution: Fedora 9
Posts: 158

Original Poster
Rep: Reputation: 30
Perfect, I should be able to do what I need with the data in an array. Thanks graemef!
 
Old 11-01-2006, 03:14 PM   #4
PeO
Member
 
Registered: Nov 2002
Location: Karlsborg/Sweden
Distribution: Slackware 13.1, Mint, Ubuntu, Linux since ~1998
Posts: 60

Rep: Reputation: 15
I have done it that way but it wouldn't work...

$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....
 
Old 11-01-2006, 05:26 PM   #5
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
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";
... 
 
Old 11-02-2006, 01:26 PM   #6
PeO
Member
 
Registered: Nov 2002
Location: Karlsborg/Sweden
Distribution: Slackware 13.1, Mint, Ubuntu, Linux since ~1998
Posts: 60

Rep: Reputation: 15
Thx it worked very well !!
 
Old 11-02-2006, 04:58 PM   #7
PeO
Member
 
Registered: Nov 2002
Location: Karlsborg/Sweden
Distribution: Slackware 13.1, Mint, Ubuntu, Linux since ~1998
Posts: 60

Rep: Reputation: 15
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]...
 
Old 11-02-2006, 05:06 PM   #8
PeO
Member
 
Registered: Nov 2002
Location: Karlsborg/Sweden
Distribution: Slackware 13.1, Mint, Ubuntu, Linux since ~1998
Posts: 60

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


Reply

Tags
php



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
File recovery after partition table damage - XFS file system gracecourt Linux - General 4 09-28-2017 05:29 PM
PHP - Help With Reading Lines In A File windisch Programming 6 05-03-2006 12:48 PM
PHP/MySQL table question newuser455 Programming 10 11-03-2005 04:50 AM
using DD to pull records out of a data file fhinkle Linux - Newbie 6 02-24-2005 04:20 PM
PHP table color change agallant Programming 1 07-16-2004 08:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:46 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration