Thread View: alt.comp.lang.php
4 messages
4 total messages
Started by Jan-
Wed, 23 May 2001 22:38
How do I do this?
Author: Jan-
Date: Wed, 23 May 2001 22:38
Date: Wed, 23 May 2001 22:38
17 lines
1644 bytes
1644 bytes
I want to display only the last line of a txt-file. Ex. I have a txt-file named 'lines.txt' and it contains this: This is line one This in line two This is line three Now I wanna make a php script that only displays the last line so only 'This is line three'. How do I do this, is it possible? Regards, Jan
Re: How do I do this?
Author: jbp
Date: Wed, 23 May 2001 22:50
Date: Wed, 23 May 2001 22:50
30 lines
1780 bytes
1780 bytes
===== Original Message from: Jan- ===== >I want to display only the last line of a txt-file. >Ex. I have a txt-file named 'lines.txt' and it contains this: > >This is line one >This in line two >This is line three > > >Now I wanna make a php script that only displays the last line so only 'This >is line three'. >How do I do this, is it possible? <?php $lines = file(lines.txt); echo $lines[sizeof($lines)-1]; ?> or <?php $lines = file(lines.txt); echo $lines[sizeof($lines)]; ?> depending on whether your text file has a trailing cr/lf. -- Joseph Birr-Pixton .:|:. http://ifihada.com .:|:. ICQ#40675236
Re: How do I do this?
Author: Darryl Catchpole
Date: Wed, 23 May 2001 22:53
Date: Wed, 23 May 2001 22:53
24 lines
1902 bytes
1902 bytes
On Wed, 23 May 2001, Jan- wrote:
> I want to display only the last line of a txt-file.
> Ex. I have a txt-file named 'lines.txt' and it contains this:
>
> This is line one
> This in line two
> This is line three
>
>
> Now I wanna make a php script that only displays the last line so only 'This
> is line three'.
> How do I do this, is it possible?
<?
$foo = file("contact.html");
$lastline = count($foo) - 1;
echo "Lastline $lastline : " . $foo[$lastline] . "<br>\n";
?>
Re: How do I do this?
Author: Egbert Teeselink
Date: Thu, 24 May 2001 08:50
Date: Thu, 24 May 2001 08:50
27 lines
2017 bytes
2017 bytes
> <?php
> $lines = file(lines.txt);
> echo $lines[sizeof($lines)-1];
> ?>
>
> or
>
> <?php
> $lines = file(lines.txt);
> echo $lines[sizeof($lines)];
> ?>
>
> depending on whether your text file has a trailing cr/lf.
could be combined as
<?php
$lines = file("lines.txt"); //you forgot the quotes here :)
$sf = sizeof($lines);
echo (empty(trim($lines[$sf])) ? $lines[$sf - 1] : $lines[$sf]);
?>
making it not matter anymore whether the file has a cr/lf at the end or not.
(note, only having whitespace characters on the last line, which should be
echoed instead of ignored, will be heavily screwed up by this)
skrebbel.
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads