Hi - slightly related to my other topic posted below, but hopefully quicker to answer - how do you add a carriage return followed by a line feed (i.e ASCII char 13 followed by ASCII char 10) to create a new line in a csv file, so that each time you append the file you create a new line?

The csv file is written using fwrite via PHP.

Many thanks,

    It has been a while since I have done the windows thing, but if I recall correctly, its just \r\n

      Thanks for the reply,

      I'm creating a variable that writes to the csv file - here's a snippet from the last part of the variable - do I just add the newline command you suggested like this?:

      $content .= $_POST[code14];
      $content .='"';
      $content .=',';
      $content .=',';
      $content .='"';
      $content .= $_POST[code15];
      $content .='"';
      $content .=',';
      $content .='\r\n';
      

      Regards,

        that should do the trick. This is what I found in user comments on PHP.net

        Hope this helps other newbies.

        If you are writing data to a txt file on a windows system and need a line break. use \r\n . This will write hex OD OA.

        i.e.
        $batch_data= "some data... \r\n";
        fwrite($fbatch,$batch_data);

        The is the equivalent of opening a txt file in notepad pressing enter and the end of the line and saving it.

          Many thanks for that, I read up on the fopen section of php.net which filled me in a little further.

          When I view my csv file in notepad however it just shows "\r \n" and then continues the next entry on the same line - its not actually seeing the commands as a 'newline',
          for example, here's an extract from my csv showing the end of one file and the start of another:

          "test",22,"end",\r\n777777777,"test2","test","test",,

          Any ideas?

            I believe in order for it to work it must be in double-quotes (as maxpup979 shows), not single quotes.

            So your last line of code would be:

            $content .="\r\n";

            if that doesn't work, try just this:

            $content .="\n";

              So sorry - you are both correct. I had been using single quotes as I needed to include double quotes as part of a string and didn't want to clutter up the code with escape chars.

              Thanks to your advice the newlines now work.

              Many thanks indeed.

                No, problemo. Single quotes are easier to type, too! 🙂

                  Write a Reply...