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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Mar 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    PHP - file_put_contents - put new line above content

    Hello, i am new here and i have - maybe simple - question. How to put new line to txt file (file_put_contents - FILE_APPEND) above the text. My script puts new line under the contents and that's not good for me.
    Thank you

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    Show us your script.

  • #3
    New to the CF scene
    Join Date
    Mar 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    <?php
    include('class-search.php');
    $html = file_get_html('http://deluxetelevision.com/deluxe-radio/');
    $polozka = $html->find('span.playlist_stunde_time');
    foreach($polozka as $playlist)
        echo $playlist->plaintext . '<br />';
    
    $file = 'playlist.txt';
    foreach($polozka as $playlist)
      {
    	file_put_contents($file, $playlist->plaintext ."\n", FILE_APPEND);
      }
    ?>

  • #4
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    The simplest way would be to do a file_put_contents outside of the foreach however you may need to truncate the file first and then use the foreach to append the contents.
    ||||If you are getting paid to do a job, don't ask for help on it!||||


  •  

    Posting Permissions

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