Reading lines from a text file.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
srirams
Forum Commoner
Posts: 36
Joined: Wed May 25, 2005 8:57 am
Location: India
Contact:

Reading lines from a text file.

Post by srirams »

All,

I would like to know a method where by I can read the first line into a string variable and the rest of the lines into another variable.
For intance
$filename = 'file.txt';
$fp = fopen($filename, "r");

if file.txt has 3 lines.
line1
line2
line3
I should store line1 into one variable $string1 and store the lines (line2 and line3) together into a string variable called string2.
Please suggest.

Regards
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Code: Select all

$lines = file_get_contents('file.txt');
Now all the lines are in $lines[0], $lines[1], ... $lines[count($lines)-1]

Code: Select all

for ($i = 0; $i < count($lines); ++$i)
{
  ${"line" . $i } = $lines[$i];
}
And now they are in $line0, $line1, ...
Syranide
Forum Contributor
Posts: 281
Joined: Fri May 20, 2005 3:16 pm
Location: Sweden

Post by Syranide »

no, file does that
file_get_contents returns all "as-is" in a string.
srirams
Forum Commoner
Posts: 36
Joined: Wed May 25, 2005 8:57 am
Location: India
Contact:

Post by srirams »

Hi,

But this will not store even the first line together.
I want the first line in a different string and the 2nd and 3rd into another string.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Meaby need to correct the syntax a little, but here is the idea:

Code: Select all

$fp = fopen('file.txt');
$i = 1;
while ($line = fread($fp, 2048))
{
  ${"line" . $i} = $line;
  ++$i;
}
fclose($fp);
srirams
Forum Commoner
Posts: 36
Joined: Wed May 25, 2005 8:57 am
Location: India
Contact:

Post by srirams »

How do I put line2, line3 and line4 into one string?

say
Cricket.
Warne the best. Murli is 2nd. Kumble might be 3rd.

Now cricket should be stored in string named $string1
Warne the best. Murli is 2nd. Kumble might be 3rd. these 3 lines should be stored together in $string2.

Hope this is clear now.
djot
Forum Contributor
Posts: 313
Joined: Wed Jan 14, 2004 10:21 am
Location: planet earth
Contact:

Post by djot »

This only works, if your file always has the structure you described (first line => string1, second-third-forth line => string2)

Code: Select all

$i = 1;
$string1='';
$string2='';

$fp = fopen('file.txt');
while ($line = fread($fp, 2048))
{
  if ($i==1) { $string1 = $line; }
  else { $string2 .= $line; }
  ++$i;
}
fclose($fp);
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Here's my 2 cents:

Code: Select all

$file_contents = file('file.txt');
$string1 = current($file_contents);
while($string2 .= next($file_contents))
{
  //this is actually an empty loop, as the work is done in the condition
}
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
djot
Forum Contributor
Posts: 313
Joined: Wed Jan 14, 2004 10:21 am
Location: planet earth
Contact:

Post by djot »

You did spend many bytes for your comment in relation to the code length :)
Post Reply