Read a line of last input to a text file

Started on the old forum in January 2013 · view the original in the archive · old links redirect here

13 replies · 3,583 views · started 13 years ago · 2 min read

TL;DR · summary of 13 replies
  • mantraJAYBEE found the solution with Ghost's approach using array reverse.
  • The code for reading the last line from a text file, including handling symbols, was provided by Ghost and modified slightly by arnage.

Written automatically from the replies by a language model on our own server. It can be wrong; the posts below are the source.

3 participants
mantraJAYBEE Newbie · 8 posts #1

I have a text file with the following:

JAR |Welcome post your comment/request what you want one .smile. |127.0.0.1|Aug 22nd, 2012 - 10:55pm|jarloft|blaaaaa
Hackyboy|Hey,this is just wat I thot! .lol.|127.0.0.1|Aug 22nd, 2012 - 10:59pm|jarloft.tk|jaysean@gamil.com
Hackyboy|Welcum to the home of mobile downloads .smiles. |82.145.208.33|Aug 23rd, 2012 - 9:38pm|jarloft.tk|Hackyboy@fine.tk
Young|Plz .sad. I need 2go + Opera ScreenShort @HACKYBOY help me out|82.145.208.171|Aug 25th, 2012 - 3:42pm|jarloft.tk|Hacky@gmail.com
JAR|Are here to put smile .smile. in your face by uploading all requested so dont be sad .sad. becouse we have been busy fixing some errors|65.255.37.191|Sep 3rd, 2012 - 8:38pm|mega.tk|go@u.co
JARLOFT|We Are here to put smile .smile. in your face by uploading all requested so dont be sad .sad. becouse we have been busy fixing some errors|65.255.37.191|Sep 3rd, 2012 - 8:42pm|mega.tk|g@m.co
Deva|Please give php script like yours|116.202.134.57|Sep 4th, 2012 - 8:01am||obe@gmail.com
JAR|All ready uploaded|82.145.208.31|Sep 6th, 2012 - 2:08am|jar.tk|H@m.co
H4hope|.lol. Pls i need d script 4 reg,profile,login and forum dat i can use in any site|82.145.217.49|Sep 10th, 2012 - 3:04am|naija.tk|Incredible@yahoo.com
JAR|Ok, dats is html work with one trick i will upload it|82.145.209.178|Sep 11th, 2012 - 8:00am|jar.tk|H@c.me
Ken|Pls naijahost.tk is my webhost site and i login to my control panel after register but cant find the FTP access and details|141.0.8.192|Sep 24th, 2012 - 7:35pm|ken.tk|Kenn@gmail.com
Blog|Hai... Join me http://m.de|82.145.210.126|Sep 25th, 2012 - 3:29pm|m.de|Admin@blog.es
Deva|Please upload any php website|65.255.37.187|Sep 26th, 2012 - 6:44am||obet@gmail.com
JAR|KENNERYZ go to our forum http://9j.tk DAVE check the site|65.255.37.195|Sep 27th, 2012 - 1:14pm||Ha@co.com
Cl***ic|.sad.|82.145.216.71|Sep 29th, 2012 - 6:36am|cod.tk|Ad@yahoo.com
Cl***ic|Pls i nid waploaded script|82.145.216.71|Sep 29th, 2012 - 6:37am|cd.tk|Adenu@yahoo.com
Admin|Welcome .smile.|217.212.230.214|Jan 3rd, 2013 - 9:25pm|grandwap.tk|mjb@gmail.com

and using fopen I want to show ONLY the last input as: echo '$lastu: $lastmsg'; excluding time and IP address.. i.e Admin: Welcome Can anybody pls help me?

arnage Lead · 811 posts #2

Consider that in part of writing into file add "\n" at the end, that way its gonna pack it line by line and would be much easier to read just one line. In that case you can count lines and take one from the beginning of the file or from its end, depending to where its writing into file.

<!DOCTYPE html PUBLIC "-//***WAPFORUM**.**RS***
mantraJAYBEE Newbie · 8 posts #3

But there's a way to read it as it is.. I've seen it somewhere

Ghost Senior · 366 posts #4
<?php
$File = 'test.txt';
$End_File = file($File);
for($k = 0; $k<count($End_File); $k++)
{/** we do nothing here, we just wanted to get to end of file for info. */}
preg_match('/(.*?)\|(.*?)\|(.*?)\|(.*?)\|(.*?)\|(.*?)/i', $End_File[$k-1], $matches);
$Latest_Post = $matches[1].': '.$matches[2];
echo $Latest_Post;
?>
///
//

and if you wanted to read the info backwards. you could use array reverse

$End_File = array_reverse(file($File));
///
//

heres a temp link to it working. http://codemafia.uk.to/test.php

how it works. used count to get to end of file. used preg match to match the data input set with | then echo'd first 2 matches only. name/message.

<?php include ('Ghost'); if ($Post == true) { echo ' ![](http://destrukt.dk/ico/phpdeveloper.jpg) alt='coding-talk.com!!' />'; echo 'Sharing Is Caring!'; } else { echo ' ![](http://img98.imageshack.us/img98/6476/av3ro4.jpg) alt='the username GHOST has been comprimised!' />'; echo 'OMG SOMEBODY HELP ME!!'; } ?>
mantraJAYBEE Newbie · 8 posts #5

Ghost wrote:

<?php
$File = 'test.txt';
$End_File = file($File);
for($k = 0; $k<count($End_File); $k++)
{/** we do nothing here, we just wanted to get to end of file for info. */}
preg_match('/(.*?)\|(.*?)\|(.*?)\|(.*?)\|(.*?)\|(.*?)/i', $End_File[$k-1], $matches);
$Latest_Post = $matches[1].': '.$matches[2];
echo $Latest_Post;
?>
///
//

and if you wanted to read the info backwards. you could use array reverse

$End_File = array_reverse(file($File));
///
//

heres a temp link to it working. http://codemafia.uk.to/test.php

how it works. used count to get to end of file. used preg match to match the data input set with | then echo'd first 2 matches only. name/message.

Thanks Ghost bro.. Thats the exact code I wanted!

Ghost Senior · 366 posts #6

your very welcome.

<?php include ('Ghost'); if ($Post == true) { echo ' ![](http://destrukt.dk/ico/phpdeveloper.jpg) alt='coding-talk.com!!' />'; echo 'Sharing Is Caring!'; } else { echo ' ![](http://img98.imageshack.us/img98/6476/av3ro4.jpg) alt='the username GHOST has been comprimised!' />'; echo 'OMG SOMEBODY HELP ME!!'; } ?>
mantraJAYBEE Newbie · 8 posts #7

Ghost wrote: your very welcome.

the shoutbox ***** is as a result of ur code.. I later spiced it up with substr(); for emotions and wordwrap to manage page width.. Wish I can find a good logo maker now

Ghost Senior · 366 posts #8

nice work. happy to help. didnt take me long to make the piece of code as part of it was already in my flatfile forum project code ive been writing up.

happy to help.

<?php include ('Ghost'); if ($Post == true) { echo ' ![](http://destrukt.dk/ico/phpdeveloper.jpg) alt='coding-talk.com!!' />'; echo 'Sharing Is Caring!'; } else { echo ' ![](http://img98.imageshack.us/img98/6476/av3ro4.jpg) alt='the username GHOST has been comprimised!' />'; echo 'OMG SOMEBODY HELP ME!!'; } ?>
arnage Lead · 811 posts #9

In the case of adding \n at writing...

foreach (explode('|', end(file('file.txt'))) as $k => $v) echo $v.'<br/>';

🙂

<!DOCTYPE html PUBLIC "-//***WAPFORUM**.**RS***
mantraJAYBEE Newbie · 8 posts #10

arnage wrote: In the case of adding \n at writing...

foreach (explode('|', end(file('file.txt'))) as $k => $v) echo $v.'<br/>';

:)

That's basic.. I was just confused in the case of many symbols.. Thanx anyway..!

arnage Lead · 811 posts #11

Yeah, sure. 😉

<!DOCTYPE html PUBLIC "-//***WAPFORUM**.**RS***
Ghost Senior · 366 posts #12

arnage wrote: In the case of adding \n at writing...

foreach (explode('|', end(file('file.txt'))) as $k => $v) echo $v.'<br/>';

:)

i believe his code for writting the data to text file already adds the \n for new lines. his text file he supplied for this was actually all on new lines which you wouldnt see as \n is invisible once added to txt file.. it just doesnt look like here on forum due to forum codebox wordwrapping the data when displayed. 😉

<?php include ('Ghost'); if ($Post == true) { echo ' ![](http://destrukt.dk/ico/phpdeveloper.jpg) alt='coding-talk.com!!' />'; echo 'Sharing Is Caring!'; } else { echo ' ![](http://img98.imageshack.us/img98/6476/av3ro4.jpg) alt='the username GHOST has been comprimised!' />'; echo 'OMG SOMEBODY HELP ME!!'; } ?>
arnage Lead · 811 posts #13

I can't believe, i need the code to see it and by the posted content of the file i just assumed that. Thats why i insist on posting codes when asking for help about the same code. In the future i will delete any such thread as it is stupid, i doubt that here are some psychics... Anyway, even better if it does add new line already. 🙂

<!DOCTYPE html PUBLIC "-//***WAPFORUM**.**RS***
Ghost Senior · 366 posts #14

arnage wrote: I can't believe, i need the code to see it and by the posted content of the file i just assumed that. Thats why i insist on posting codes when asking for help about the same code. In the future i will delete any such thread as it is stupid, i doubt that here are some psychics... Anyway, even better if it does add new line already. :)

yeah at first even i didnt think it was new lines. i also thought it was 1 full line being seperated by alot of | (pipelines) but once i copied and pasted the text to my computer i noticed it was all on seperate lines and it made my job of making a piece of code to read it alot more easier. 🙂

<?php include ('Ghost'); if ($Post == true) { echo ' ![](http://destrukt.dk/ico/phpdeveloper.jpg) alt='coding-talk.com!!' />'; echo 'Sharing Is Caring!'; } else { echo ' ![](http://img98.imageshack.us/img98/6476/av3ro4.jpg) alt='the username GHOST has been comprimised!' />'; echo 'OMG SOMEBODY HELP ME!!'; } ?>

Write your reply now; next you create a free account (or sign in) and it is posted right away. Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close