Text file Reverse reading

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Faisal Shah
    New Member
    • Mar 2008
    • 14

    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!
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #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

    Comment

    • Faisal Shah
      New Member
      • Mar 2008
      • 14

      #3
      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!

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #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:
        Code:
        ABCDEFGH
        you want to read it in as left-to-right:
        Code:
        HGFEDCBA
        If that is the case and assuming you have this file content.
        Code:
        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('cod e.txt');
        foreach($file as $line)
        echo implode("",arra y_reverse(preg_ split('//', $line))).'<br>' ;
        ?>
        [/php]Ronald

        Comment

        • Faisal Shah
          New Member
          • Mar 2008
          • 14

          #5
          image breaks screen

          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:
          Code:
          ABCDEFGH
          you want to read it in as left-to-right:
          Code:
          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!
          Last edited by ronverdonk; Mar 23 '08, 10:30 AM. Reason: image removed

          Comment

          • ronverdonk
            Recognized Expert Specialist
            • Jul 2006
            • 4259

            #6
            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

            Comment

            • Faisal Shah
              New Member
              • Mar 2008
              • 14

              #7
              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 ;

              Code:
              <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

              Code:
              ..............>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 ;

              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!

              Comment

              • ronverdonk
                Recognized Expert Specialist
                • Jul 2006
                • 4259

                #8
                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

                Comment

                • ronverdonk
                  Recognized Expert Specialist
                  • Jul 2006
                  • 4259

                  #9
                  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

                  Comment

                  • Faisal Shah
                    New Member
                    • Mar 2008
                    • 14

                    #10
                    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!

                    Comment

                    • ronverdonk
                      Recognized Expert Specialist
                      • Jul 2006
                      • 4259

                      #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

                      Comment

                      • Faisal Shah
                        New Member
                        • Mar 2008
                        • 14

                        #12
                        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???

                        Code:
                        <?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!

                        Comment

                        • ronverdonk
                          Recognized Expert Specialist
                          • Jul 2006
                          • 4259

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

                          Ronald

                          Comment

                          • Faisal Shah
                            New Member
                            • Mar 2008
                            • 14

                            #14
                            Originally posted by ronverdonk
                            Since you do not write the newline, write it to your output text file like this[php]fwrite($f_handl e,$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!
                            Last edited by ronverdonk; Mar 23 '08, 04:48 PM. Reason: remove url

                            Comment

                            • ronverdonk
                              Recognized Expert Specialist
                              • Jul 2006
                              • 4259

                              #15
                              You are welcome. We'll see you around some time.

                              Ronald

                              Comment

                              Working...