469,592 Members | 1,795 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,592 developers. It's quick & easy.

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 3048
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

Post your reply

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

Similar topics

5 posts views Thread by xEM | last post: by
14 posts views Thread by Erik Andersson | last post: by
7 posts views Thread by Jay | last post: by
20 posts views Thread by sahukar praveen | last post: by
4 posts views Thread by Jeff Rodriguez | last post: by
6 posts views Thread by Rajorshi Biswas | last post: by
6 posts views Thread by Neil Patel | last post: by
2 posts views Thread by RobcPettit | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.