473,770 Members | 2,630 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Text file Reverse reading

14 New Member
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 3546
ronverdonk
4,258 Recognized Expert Specialist
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 Shah
14 New Member
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 Recognized Expert Specialist
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('cod e.txt');
foreach($file as $line)
echo implode("",arra y_reverse(preg_ split('//', $line))).'<br>' ;
?>
[/php]Ronald
Mar 23 '08 #4
Faisal Shah
14 New Member
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 Recognized Expert Specialist
Just reverse the array into which the file contents are stored line by line, like[php]<?php
$file=file('cod e.txt');
$lines = array_reverse($ file);
foreach ($lines as $line)
echo $line . ".<br>";
?>[/php]Ronald
Mar 23 '08 #6
Faisal Shah
14 New Member
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 Recognized Expert Specialist
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 Recognized Expert Specialist
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
Faisal Shah
14 New Member
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

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

Similar topics

5
14918
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 file (line ends with "\n") ? -- pozdr. xEM~
14
13733
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? The solution has to be platform independent, so "popen("tac $filename")... wouldn't work.
7
17463
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 backwards, starting with the last line. Previously, I did: myfile = open('myfile.txt', 'r') mylines = myfile.readlines() myfile.close()
20
33075
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. From here read a character, print it, move the file pointer (FILE*) to 2 steps back (using fseek(fp, -2, SEEK_CUR)) to read the previous character. This seems to be ok if the file has a single line (i.e. no new line character). The above logic...
4
7260
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
6372
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 browsing through this group and other sources on the web, it seems that there are many ways to do it. Some suggest that simply fseek'ing to 8K bytes before the end of file, and going backwards is the way. In this case, am I guaranteed best results...
6
44110
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
3590
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 order starting with the last line first..... Any assistance would be greatly appreciated.... Thanks, Chris
2
1642
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 to. Ive written some code myself which uses streemreader to read the file into an array, which I then reverse as I only want the last 10-15 lines entered in the file. I fire this code using a timer. My problem is this: If the betfair software is...
11
4217
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 can't seem to figure out how to do it one line at a time. Here is what I'm trying to do: I want the user to be able to print the text file one line at a time by clicking a button to see the next line. Example: If text_file1 first line is...
0
9602
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9439
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10237
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10017
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9882
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8905
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7431
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6690
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3589
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.