Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

PHP trim help!

  • 11-10-2001 10:47PM
    #1
    Closed Accounts Posts: 59 ✭✭


    Could someone tell me how to get rid of the end of line (\n) when reading from an external .txt file ?


    I think it should go something like this:

    <?
    $file_dir = 'path/to';
    $text = file("$file_dir/file.txt");
    $textline = $text[0];
    $trimmed = rtrim($textline);

    echo $trimmed;
    ?>

    ...unfortunately I'm getting an error.



    Thanks in advance,
    Peter.


Comments

  • Registered Users, Registered Users 2 Posts: 1,562 ✭✭✭Snaga


    I used............................

    <?
    $file_path = 'test.txt';
    $text = file("$file_path");
    $textline = $text[0];
    $trimmed = rtrim($textline);

    echo $trimmed;
    ?>


    This works fine for me where the text file is as follows(in the same directory as the php script)

    --> cat test.txt
    blah
    blah2
    blah3

    It outputs 'blah' as it should.


  • Banned (with Prison Access) Posts: 16,659 ✭✭✭✭dahamsta


    There's nothing wrong with your code Peter, apart from verbosity:

    [PHP]
    <?
    $t = file('path/to/file.txt');
    echo rtrim($t[0]);
    ?>
    [/PHP]

    What's the error?

    adam


Advertisement