Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Newline character

Posted on 2004-04-30
10
Medium Priority
?
50,514 Views
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?

Thanks

ED
0
Comment
Question by:Edeldragon
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +3
10 Comments
 
LVL 18

Accepted Solution

by:
CrYpTiC_MauleR earned 200 total points
ID: 10958024
use \r\n
0
 
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.
0
 
LVL 32

Expert Comment

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

http://www.karakas-online.de/EN-Book/include-plain-text-file-in-module.html
http://www.php.net/manual/en/function.nl2br.php

All the Best.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:BrianPap22
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.
0
 
LVL 4

Expert Comment

by:BrianPap22
ID: 10958050
you people... what are you all doing awake? go to bed so i can get points. sheesh!
0
 
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...
0
 

Author Comment

by:Edeldragon
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.
0
 
LVL 18

Expert Comment

by:CrYpTiC_MauleR
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)
0
 
LVL 4

Expert Comment

by:BrianPap22
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.
0
 

Expert Comment

by:jv2222
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:

<?php

$content = <<<END
name,age,sex
bob,22,male
sue,24,female
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 (,)

Cheers,
Justin
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

715 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