Newline character

Posted on 2004-04-30
Last Modified: 2013-12-03
Hi Experts

I'm exporting a report from my database in various file formats. One of them is CSV. I'm using:

header("Content-type: application/notepad");
header("Content-Disposition: attachment;filename="Export.txt");

to get Explorer to ask if the file should be saved to disk, or opened from the location. Finally, to the question: I'm using "\n" for the linebreaks, but they aren't appearing when I open the saved file in Notepad, it just gives me reams of continuous text. However, if I print the file, or open it in Excel, then I get the correctly formatted data. It's just notepad that doesn't show the linebreak correctly. Any ideas on what character I should be using?


Question by:Edeldragon
  • 3
  • 2
  • 2
  • +3
LVL 18

Accepted Solution

CrYpTiC_MauleR earned 50 total points
ID: 10958024
use \r\n
LVL 25

Expert Comment

by:Marcus Bointon
ID: 10958035
DOS/Windows line breaks are not just a single character. You should be using "\r\n" as the line break for Windows (also known as CRLF, short for carriage return, line feed). Unix-like systems use "\n", MacOS 9 uses "\r", OS X can cope with either "\n" or "\r".
It's fairly likley that Excel is clever enough to convert/accept line breaks in vaying formats, but notepad probably doesn't - it isn't exactly the height of text-processing pile.
LVL 32

Expert Comment

ID: 10958037
might be helpful....its not simply writing into a notepad with newline, but something interesting you can learn from...

All the Best.
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.


Expert Comment

ID: 10958040
Unix uses \n to signify a newline.  Mac uses \r (carriage return), and for some bizarre reason, Windows uses both... so you would need to do "\r\n"

If you just want to view the data, then if you open it in Wordpad instead of Notepad it'll honor the lone \n as being a newline (if you edit it in Notepad or Wordpad, it will add \r\n to any newlines you make with it.

Expert Comment

ID: 10958050
you people... what are you all doing awake? go to bed so i can get points. sheesh!
LVL 25

Expert Comment

by:Marcus Bointon
ID: 10958115
Ah, well, I'm kind of cheating being on the other side of the world from you! It's 11:45am in London...

Author Comment

ID: 10958190
3 answers within 4 minutes! Not too shabby. Thanks. I had to give it to Cryptic Mauler because his was the first correct one submitted, although I suspect that Squinky and BrianPap22 only lost out because they were writing me long answers!

Thanks again.
LVL 18

Expert Comment

ID: 10959585
mmmm timing =oP, all I did was get right down to answering the question : Any ideas on what character I should be using?
But tis was a close race still =oPm Thanks!! =o)

Expert Comment

ID: 10964378
Hey, give a man a fish, feed him for a day. Teach a man that he can either use \r\n (CRLF) or just view the file in Wordpad instead of Notepad, and now everyone is happy.

Expert Comment

ID: 10988262
If you are using a PC and you would like the file to be streamed DIRECTLY into excel (i.e. automatically open Excel) you can do the following:


$content = <<<END

            header("Content-Length: ".strlen($content));
            header("Content-Disposition: attachment; filename=your_file.xls");
            header("Content-Type: application/octet-stream");
            header("Content-Transfer-Encoding: binary");

            print $content;


Note: You can also use \t instead of commas (,)


Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit ( and similar technologies have enjoyed wide adoption, making it possib…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question