Text file Reverse reading | Newbie | | Join Date: Mar 2008
Posts: 14
| | |
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!
|  | Moderator | | Join Date: Jul 2006 Location: The Netherlands
Posts: 4,139
| | | re: Text file Reverse reading
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
| | Newbie | | Join Date: Mar 2008
Posts: 14
| | | re: Text file Reverse reading Quote:
Originally Posted by ronverdonk 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!
|  | Moderator | | Join Date: Jul 2006 Location: The Netherlands
Posts: 4,139
| | | re: Text file Reverse reading
Do you mean that you want ASCII characters to be read from right to left? Like when the text line says left-to-right:
you want to read it in as left-to-right:
If that is the case and assuming you have this file content. - abcdefghijkl-1
-
dfgheritops-2
-
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
| | Newbie | | Join Date: Mar 2008
Posts: 14
| | | re: Text file Reverse reading Quote:
Originally Posted by ronverdonk Do you mean that you want ASCII characters to be read from right to left? Like when the text line says left-to-right:
you want to read it in as left-to-right:
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!
|  | Moderator | | Join Date: Jul 2006 Location: The Netherlands
Posts: 4,139
| | | re: Text file Reverse reading
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
| | Newbie | | Join Date: Mar 2008
Posts: 14
| | | re: Text file Reverse reading
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 ; -
<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 -
..............>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 ; -
1.
-
<?php
-
2.
-
$file=file('code.txt');
-
3.
-
foreach($file as $line)
-
4.
-
echo implode("",array_reverse(preg_split('//', $line))).'<br>';
-
5.
-
?>
-
Nothing Changes :( It gust process the file like normal.. Not in reverse order :(
Help please ...
FAISAL!
|  | Moderator | | Join Date: Jul 2006 Location: The Netherlands
Posts: 4,139
| | | re: Text file Reverse reading Quote:
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
|  | Moderator | | Join Date: Jul 2006 Location: The Netherlands
Posts: 4,139
| | | re: Text file Reverse reading Quote:
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
| | Newbie | | Join Date: Mar 2008
Posts: 14
| | | re: Text file Reverse reading Quote:
Originally Posted by ronverdonk 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!
|  | Moderator | | Join Date: Jul 2006 Location: The Netherlands
Posts: 4,139
| | | re: Text file Reverse reading
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
| | Newbie | | Join Date: Mar 2008
Posts: 14
| | | re: Text file Reverse reading Quote:
Originally Posted by ronverdonk 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??? -
<?php
-
-
//Fsoft ShoutBox 1.0 Faisal Shah's Edition
-
//Faisal Shah => 2:48PM 22 March 2008 French Standard time (+1GMT)
-
//(c)Fsoft Software Development (Shout Box) 2008 All Rights Reserved.
-
-
$dbname = "shoutboxdb.txt";
-
$name = $_POST["name"];
-
$web_site = $_POST["web_site"];
-
$email = $_POST["email"];
-
$message = $_POST["message"];
-
$date = date('m/d/Y H:i:s');
-
$ip = $_SERVER['REMOTE_ADDR'];
-
$ip_final = "$ip";
-
$send = $_POST["send"];
-
$new_line = "<br>..............";
-
$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>";
-
-
if($send == Shout){
-
if($name == "")
-
{
-
echo "Please insert valid name";
-
@include('form.html');
-
}elseif($message == "")
-
{
-
echo "Please insert message into message field..";
-
@include('form.html');
-
}else
-
{
-
-
$f_handle = fopen($dbname,'a');
-
fwrite($f_handle,$full_message.$new_line);
-
fclose($f_handle);
-
-
echo "<i>Message Sent....</i>";
-
@include('form.html');
-
}
-
}else
-
{
-
@include('form.html');
-
}
-
?>
-
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!
|  | Moderator | | Join Date: Jul 2006 Location: The Netherlands
Posts: 4,139
| | | re: Text file Reverse reading
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
| | Newbie | | Join Date: Mar 2008
Posts: 14
| | | re: Text file Reverse reading Quote:
Originally Posted by ronverdonk 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!
|  | Moderator | | Join Date: Jul 2006 Location: The Netherlands
Posts: 4,139
| | | re: Text file Reverse reading
You are welcome. We'll see you around some time.
Ronald
|  | | | | Forums
Visit our community forums for general discussions and latest on Bytes
/bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 229,155 network members.
|