473,326 Members | 2,133 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Text file Reverse reading

Hi,

Well, My name is Faisal, You might saw as my ID. I got into php before a few months...

I need help.. I am trying to read a file that is .txt format..
Line by Line, Reverse action..

I mean from Last to 1st line.. I really don't care.. I can write them on my memory of php.. No matter..

Can you advice me how can I do this work?

I mean if i can do from an array.. So can you guide me how? I am not so good in arrays ;)

Help please...

Waiting for answer!

FAISAL!
Mar 22 '08 #1
14 3518
ronverdonk
4,258 Expert 4TB
Faisal, welcome to The Scripts.

Reading an entire file in one go into an array (1 line per entry) might be the easiest wsy of doing this. Therefore you use the PHP file() command. See documention file() command

Ronald
Mar 22 '08 #2
Faisal, welcome to The Scripts.

Reading an entire file in one go into an array (1 line per entry) might be the easiest wsy of doing this. Therefore you use the PHP file() command. See documention file() command

Ronald
Thanks Ronald,
Well, as i already said I am so bad in looping :(
Can you please write a small code in which i can read the content of "shoutboxdb.txt" file in reverse Action???

Thanks..

FAISAL!
Mar 23 '08 #3
ronverdonk
4,258 Expert 4TB
Do you mean that you want ASCII characters to be read from right to left? Like when the text line says left-to-right:
Expand|Select|Wrap|Line Numbers
  1. ABCDEFGH
you want to read it in as left-to-right:
Expand|Select|Wrap|Line Numbers
  1. HGFEDCBA
If that is the case and assuming you have this file content.
Expand|Select|Wrap|Line Numbers
  1. abcdefghijkl-1
  2. dfgheritops-2
  3. nxvvq345qms-3
and you wish to read it in as I just asked you. The code would be something like[php]<?php
$file=file('code.txt');
foreach($file as $line)
echo implode("",array_reverse(preg_split('//', $line))).'<br>';
?>
[/php]Ronald
Mar 23 '08 #4
Do you mean that you want ASCII characters to be read from right to left? Like when the text line says left-to-right:
Expand|Select|Wrap|Line Numbers
  1. ABCDEFGH
you want to read it in as left-to-right:
Expand|Select|Wrap|Line Numbers
  1. HGFEDCBA
Ronald
You answered? So nice of you ;)

Well, My bro. I want to read them normally... But like this.. See here in Image..
image brakes page, replaced by text - moderator

This is the first line
And this the second
Last line

Want to read them as

Last line
And this the second
This is the first line

Thanks my Friend...
I am waiting for help badly..

FAISAL!
Mar 23 '08 #5
ronverdonk
4,258 Expert 4TB
Just reverse the array into which the file contents are stored line by line, like[php]<?php
$file=file('code.txt');
$lines = array_reverse($file);
foreach ($lines as $line)
echo $line . ".<br>";
?>[/php]Ronald
Mar 23 '08 #6
Well, Thanks.. And sorry about posting.. I didn't knew if it's not allowed to post Images here..

Well, In real my file is this ;

Expand|Select|Wrap|Line Numbers
  1. <br><a target="blank" href="http://">Faisal</a>    <a target="blank" href="http://whois.domaintools.com/127.0.0.1"> 03/23/2008 01:52:35</a> <br> <b>Beta Testing.. Just like that</b><br>..............<br><a target="blank" href="http://ddddddddddddd">dddddd</a>  dddddddddddd  <a target="blank" href="http://whois.domaintools.com/127.0.0.1"> 03/23/2008 11:24:49</a> <br> <b>dddddddddddd</b><br>..............<br><a target="blank" href="http://ddddddddddddd">dddddd</a>  dddddddddddd  <a target="blank" href="http://whois.domaintools.com/127.0.0.1"> 03/23/2008 11:24:52</a> <br> <b>dddddddddddd</b><br>..............<br><a target="blank" href="http://ddddddddddddd">dddddd</a>  dddddddddddd  <a target="blank" href="http://whois.domaintools.com/127.0.0.1"> 03/23/2008 11:25:25</a> <br> <b>dddddddddddd</b><br>..............<br><a target="blank" href="http://">dddddddddddddd</a>    <a target="blank" href="http://whois.domaintools.com/127.0.0.1"> 03/23/2008 11:29:09</a> <br> <b>dddddddddddddd</b><br>..............<br><a target="blank" href="http://">love</a>    <a target="blank" href="http://whois.domaintools.com/127.0.0.1"> 03/23/2008 11:29:18</a> <br> <b>love</b><br>..............<br><a target="blank" href="http://">love</a>    <a target="blank" href="http://whois.domaintools.com/127.0.0.1"> 03/23/2008 11:29:39</a> <br> <b>love</b><br>..............
As you can see, that the file have got some HTML Tags.. When i use http://www.thescripts.com/forum/post3125674-4.html .. What happens is IT spits out the text like this

Expand|Select|Wrap|Line Numbers
  1. ..............>rb<>b/b< >rb< >a/<14:73:11 8002/32/30 >"1.0.0.721/moc.slootniamod.siohw//:ptth"=ferh "knalb"=tegrat a< >a/"//:ptth"=ferh "knalb"=tegrat a<>rb<..............>rb<>b/b< >rb< >a/<93:92:11 8002/32/30 >"1.0.0.721/moc.slootniamod.siohw//:ptth"=ferh "knalb"=tegrat a< >a/"//:ptth"=ferh "knalb"=tegrat a<>rb<..............>rb<>b/b< >rb< >a/<81:92:11 8002/32/30 >"1.0.0.721/moc.slootniamod.siohw//:ptth"=ferh "knalb"=tegrat a< >a/"//:ptth"=ferh "knalb"=tegrat a<>rb<..............>rb<>b/b< >rb< >a/<90:92:11 8002/32/30 >"1.0.0.721/moc.slootniamod.siohw//:ptth"=ferh "knalb"=tegrat a< >a/"//:ptth"=ferh "knalb"=tegrat a<>rb<..............>rb<>b/b< >rb< >a/<52:52:11 8002/32/30 >"1.0.0.721/moc.slootniamod.siohw//:ptth"=ferh "knalb"=tegrat a< dddddddddddd >a/"ddddddddddddd//:ptth"=ferh "knalb"=tegrat a<>rb<..............>rb<>b/b< >rb< >a/<25:42:11 8002/32/30 >"1.0.0.721/moc.slootniamod.siohw//:ptth"=ferh "knalb"=tegrat a< dddddddddddd >a/"ddddddddddddd//:ptth"=ferh "knalb"=tegrat a<>rb<..............>rb<>b/b< >rb< >a/<94:42:11 8002/32/30 >"1.0.0.721/moc.slootniamod.siohw//:ptth"=ferh "knalb"=tegrat a< dddddddddddd >a/"ddddddddddddd//:ptth"=ferh "knalb"=tegrat a<>rb<..............>rb<>b/b< >rb< >a/<53:25:10 8002/32/30 >"1.0.0.721/moc.slootniamod.siohw//:ptth"=ferh "knalb"=tegrat a< >a/"//:ptth"=ferh "knalb"=tegrat a<>rb<
And when i use this code ;

Expand|Select|Wrap|Line Numbers
  1.    1.
  2.       <?php
  3.    2.
  4.       $file=file('code.txt');
  5.    3.
  6.       foreach($file as $line)
  7.    4.
  8.         echo implode("",array_reverse(preg_split('//', $line))).'<br>';
  9.    5.
  10.       ?>
  11.  
Nothing Changes :( It gust process the file like normal.. Not in reverse order :(

Help please ...

FAISAL!
Mar 23 '08 #7
ronverdonk
4,258 Expert 4TB
Well, Thanks.. And sorry about posting.. I didn't knew if it's not allowed to post Images here..
No that is okay. Of course you are allowed to post images, but this particular one 'broke' the display page to the right, so I replaced it by text that showed the same information .

moderator
Mar 23 '08 #8
ronverdonk
4,258 Expert 4TB
Nothing Changes :( It gust process the file like normal.. Not in reverse order :(
You have an input text file that contains only 1 line, as you showed! So how can the display be reversed of such a text file? In your sample you showed 3 lines, that works. But you cannot reverse the display of 1 line only.

Let me ask this differently: what do you expect that the resvere of that 1 line will be?

Ronald
Mar 23 '08 #9
You have an input text file that contains only 1 line, as you showed! So how can the display be reversed of such a text file? In your sample you showed 3 lines, that works. But you cannot reverse the display of 1 line only.

Let me ask this differently: what do you expect that the resvere of that 1 line will be?

Ronald
Opps :(
Now I understood your point...
Well, Now let me tell you what I want exactly...
I have made a shoutbox script... That is based on 4 files mainly... shoutbox.php, shoutboxdb.txt and form.html and read.php, And a hope page called main.html which contacts 2 html frames.. in 1 you can view form.html where you can find shoutbox form. And in second you can see read.php...

In read.php at this moment i simply included shoutboxdb.txt so it spits out the data what ever is shouted and processes it as it must...

But all the new shouts are added from up to down.. So when they have got too much shouts.. We can't see the newest shout.. I mean the newest shout stays at the bottom of read.php and oldest on TOP....

So now what i wanted was, reverse them OR write them in Reverse order in my shoutbox.php so the newest shout always stay on the TOP instead of Bottom :)


Help :)

FAISAL!
Mar 23 '08 #10
ronverdonk
4,258 Expert 4TB
As I see it, your text file (,txt) does not contain any newline's (\n) or carriage return chars (\r) or a combination of these.

A 'normal' text file has this at the end of each line, so when you read it in the end of each text line can be found and so the text file can be split in different lines.

Since you (as I image) do not have that, then you must determine what is the end (or beginning) of each new shoutbox entry text line. Knowing that you can spolit it up in different text lines and sort it, newest on top.

So questions are: (a) why does you text line not have an end (newline, carriage return or both) and (b) how else can we determine the beginning or end of a text line.

Ronald
Mar 23 '08 #11
As I see it, your text file (,txt) does not contain any newline's (\n) or carriage return chars (\r) or a combination of these.

A 'normal' text file has this at the end of each line, so when you read it in the end of each text line can be found and so the text file can be split in different lines.

Since you (as I image) do not have that, then you must determine what is the end (or beginning) of each new shoutbox entry text line. Knowing that you can spolit it up in different text lines and sort it, newest on top.

So questions are: (a) why does you text line not have an end (newline, carriage return or both) and (b) how else can we determine the beginning or end of a text line.

Ronald
Well, Again.. It's my first time to make a shoutbox :)

Well, can you please have a look on my shoutbox.php code here???

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. //Fsoft ShoutBox 1.0 Faisal Shah's Edition
  4. //Faisal Shah => 2:48PM 22 March 2008 French Standard time (+1GMT)
  5. //(c)Fsoft Software Development (Shout Box) 2008 All Rights Reserved.
  6.  
  7. $dbname = "shoutboxdb.txt";
  8. $name = $_POST["name"];
  9. $web_site = $_POST["web_site"];
  10. $email = $_POST["email"];
  11. $message = $_POST["message"];
  12. $date = date('m/d/Y H:i:s');
  13. $ip = $_SERVER['REMOTE_ADDR'];
  14. $ip_final = "$ip";
  15. $send = $_POST["send"];
  16. $new_line = "<br>..............";
  17. $full_message = "<br><a target=\"blank\" href=\"http://$web_site\">$name</a>  $email  <a target=\"blank\" href=\"http://whois.domaintools.com/$ip_final\"> $date</a> <br> <b>$message</b>";
  18.  
  19. if($send == Shout){
  20. if($name == "")
  21. {
  22. echo "Please insert valid name";
  23. @include('form.html');
  24. }elseif($message == "")
  25. {
  26. echo "Please insert message into message field..";
  27. @include('form.html');
  28. }else
  29. {
  30.  
  31. $f_handle = fopen($dbname,'a');
  32. fwrite($f_handle,$full_message.$new_line);
  33. fclose($f_handle);
  34.  
  35. echo "<i>Message Sent....</i>";
  36. @include('form.html');
  37. }
  38. }else
  39. {
  40. @include('form.html');
  41. }
  42. ?>
  43.  
Can you please finally fix that \n thing for new line??

Thanks..It will help me to finish this shoutbox and to learn as well ;)

FAISAL!
Mar 23 '08 #12
ronverdonk
4,258 Expert 4TB
Since you do not write the newline, write it to your output text file like this[php]fwrite($f_handle,$full_message.$new_line."\n");[/php]Then you can read the file as I showed earlier.

Ronald
Mar 23 '08 #13
Since you do not write the newline, write it to your output text file like this[php]fwrite($f_handle,$full_message.$new_line."\n");[/php]Then you can read the file as I showed earlier.

Ronald
Thanks Mr. Ronald.. It worked.. If I will have any other problem.. I will come here ;)

Thanks a Lot...

FAISAL!
Mar 23 '08 #14
ronverdonk
4,258 Expert 4TB
You are welcome. We'll see you around some time.

Ronald
Mar 23 '08 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: xEM | last post by:
how can i read for example 10 last lines from a text file beginning from last line? do you have some idea? in a different manner... how can i set file position indicator one line upper in text...
14
by: Erik Andersson | last post by:
Hi! I need to read a file (line-by-line) in reverse order, without reading the whole file into memory first. Is there an easy way of doing this? As in, is there a PHP function I could use? ...
7
by: Jay | last post by:
I have a very large text file (being read by a CGI script on a web server), and I get memory errors when I try to read the whole file into a list of strings. The problem is, I want to read the file...
20
by: sahukar praveen | last post by:
Hello, I have a question. I try to print a ascii file in reverse order( bottom-top). Here is the logic. 1. Go to the botton of the file fseek(). move one character back to avoid the EOF. 2....
4
by: Jeff Rodriguez | last post by:
How would one go about reading a file line by line in reverse? For Example: -- FILE -- 1 2 3 4 5 -- FILE --
6
by: Rajorshi Biswas | last post by:
Hi folks, Suppose I have a large (1 GB) text file which I want to read in reverse. The number of characters I want to read at a time is insignificant. I'm confused as to how best to do it. Upon...
6
by: Neil Patel | last post by:
I have a log file that puts the most recent record at the bottom of the file. Each line is delimited by a \r\n Does anyone know how to seek to the end of the file and start reading backwards?
4
by: Stupid48 | last post by:
I'm trying to do a simple task but can't seem to find a solution. How do I read lines from a text file backwards. i.e. I want to select the last 20 lines of a text file and display them in...
2
by: RobcPettit | last post by:
Hi, I have software that records betfair data into a csv file. I have no access to the code, al I can adjust is the intervals at which it records, 1sec up, and which folder/file to save the data...
11
by: JWest46088 | last post by:
I'm having difficulty trying to figure out how to print a text file from a hash table one line at a time. I have the text file read into the hash table and can print the text file all at once, but I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.