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
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,537
    Thanks
    8
    Thanked 1,093 Times in 1,084 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 02:36 AM.

  • Users who have thanked mlseim for this post:

    VirtusGraphics (08-05-2014)

  • #3
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    891
    Thanks
    174
    Thanked 102 Times in 102 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 03:15 AM.
    "The greatest revenge is to accomplish what others say you cannot do."
    ~ Unknown

    I used to be indecisive, but now I'm not so sure.

  • Users who have thanked low tech for this post:

    tangoforce (08-05-2014)

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

  • #5
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,537
    Thanks
    8
    Thanked 1,093 Times in 1,084 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 02:33 PM.

  • #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);
    ?>

  • #7
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,537
    Thanks
    8
    Thanked 1,093 Times in 1,084 Posts
    You got it.

  • #8
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,174
    Thanks
    19
    Thanked 66 Times in 65 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

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