Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Aug 2014
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    How to read a text file backwards?

    Hello forum.
    I'm new here, but I'm hoping to learn, help and get helped.
    Well, I won't make this an introduction, but here's basically the point of the thread.
    I'm not so familiar with PHP, JavaScript and so on, but I know basic HTML and CSS and can read and somewhat understand JavaScript.

    Anyway, I'm guessing that PHP is what I need to resolve this problem.

    How can I make PHP (?) read a text file and display i backward? Line break backwards.
    I'm dealing with a log file with the newest content at the bottom, and when it gets big, that's annoying when I have to scroll down when it gets big.
    Alos, I'm not the owner of this log. I have access to it trough a link, but this issue is annoying. Therefor I'd like to have something display it backwards, and can be hosted on my website.

    Example of what I mean.
    This is what the log display:
    2014-05-12 12:54:20 [T]<Mod>tasty_legend: What?
    2014-05-12 12:54:20 [CT][DarkAngels]me_sambos: make interval less
    2014-05-12 12:54:26 <Mod>tasty_legend: Not now.

    Here is how I would like it displayed:
    2014-05-12 12:54:26 <Mod>tasty_legend: Not now.
    2014-05-12 12:54:20 [CT][DarkAngels]me_sambos: make interval less
    2014-05-12 12:54:20 [T]<Mod>tasty_legend: What?

    I'd really appreciate if someone could help me with this.

  2. #2
    Supreme Master coder!
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    10,200
    Thanks
    10
    Thanked 1,166 Times in 1,157 Posts
    Try this ... using your real file instead of "yourfile.txt".

    PHP Code:
    <?php
    //  $data=file("yourfile.txt");
    $datafile_get_contents('http://www.example.com/file.txt');
    rsort($data);
    foreach(
    $data as $line){
    echo 
    $line."<br />";
    }
    ?>
    EDIT,
    I just noticed you use that file from a remote location.
    fopen() and file_get_contents() would be the way to load it, but your webhost may not allow that.

    Try it and see, otherwise, PHP CURL is needed.



    .
    Last edited by mlseim; 08-05-2014 at 01:36 AM.

  3. Users who have thanked mlseim for this post:

    VirtusGraphics (08-05-2014)

  4. #3
    Senior Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    1,053
    Thanks
    192
    Thanked 130 Times in 130 Posts
    Hi #mlseim

    I don't think this will work

    PHP Code:
    $datafile_get_contents('http://www.example.com/file.txt'); 
    rsort($data); 
    because rsort expects an array, not a string. I think you need to use explode with file_get_contents before the rsort.

    LT
    Last edited by low tech; 08-05-2014 at 02:15 AM.
    0000

  5. Users who have thanked low tech for this post:

    tangoforce (08-05-2014)

  6. #4
    New to the CF scene
    Join Date
    Aug 2014
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks a lot for the replies! I will try them out after work and come with the results

  7. #5
    Supreme Master coder!
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    10,200
    Thanks
    10
    Thanked 1,166 Times in 1,157 Posts
    I'm so used to this: $data=file("yourfile.txt");
    which is an array ... you're right, it is not an array.


    Try this instead ....

    PHP Code:
    <?php 
    //  $data=file("yourfile.txt"); 
    $datafile_get_contents('http://www.example.com/file.txt'); 
    $newdata explode("\n"$data); //create array separate by new lines
    rsort($newdata); 
    foreach(
    $newdata as $line){ 
    echo 
    $line."<br />"

    ?>
    Last edited by mlseim; 08-05-2014 at 01:33 PM.

  8. #6
    New to the CF scene
    Join Date
    Aug 2014
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Alright, the first one didn't work, but a friend gave me a code to use that did work.
    PHP Code:
    <?php
    header
    ("Content-type: text/plain");
     
    $url "http://server/log.txt";
     
    $lines explode("\n"file_get_contents($url));
    rsort($lines);
     
    $display implode("\n"$lines);
    print(
    $display);
    ?>

  9. #7
    Supreme Master coder!
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    10,200
    Thanks
    10
    Thanked 1,166 Times in 1,157 Posts
    You got it.

  10. #8
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,206
    Thanks
    19
    Thanked 68 Times in 67 Posts
    I would use array_reverse rather than rsort. rsort sorts an array in reverse alphabetical/numerical order (depending on the data). If you had several entries with the same timestamp then they would not necessarily come out in the right order. array_reverse will do exactly what you want

  11. Users who have thanked NancyJ for this post:

    low tech (08-07-2014)


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •