\r and \r\n does not work!

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Hi im trying to create a csv file from a db and insert a new line for
each row. All the resources i can find say that the use of \r or the
use of \r\n are sufficient to generate new lines. However after trying
this for many hours i have come to the conclusion that this is not the
case. Can anyone suggest a better method for generating line breaks? or
tell me what additional code i may require to create line breaks in my
csv file?



Re: \r and \r\n does not work!

Quoted text here. Click to load it

Could you please show your code sample. There might be something else that  
is causing the problem, not \r\n itself. For example there is a difference  
between using single quotes '\r\n' and double quotes "\r\n", only the latter  
form generates a linebreak.

Another situation where linebreaks are often thought to be missing, is when  
you echo something to html code with linebreaks, which mysteriously  
disappear because any linebreaks in html are interpreted as whitspace and  
therefore reduced to a single space.

<?php echo "here \r\n are \r\n some \r\n linebreaks \r\n for \r\n you"; ?>

There is also a difference between different operating systems to what is a  
linebreak. As I recall it, CR is for Mac, LF is for Linux and Windows uses a  
combination of the two, CRLF. It's one of those things I'd fix in the world  
if I had three wishes.

You might also do this:
$crlf = "
"; // now the variable $crlf contains one linebreak.

// you can insert it into doublequote strings and it works:
$character = "FredFlintstone";


"ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" -lpk
spam@outolempi.net | Gedoon-S @ IRCnet | rot13(xvzzb@bhgbyrzcv.arg)  

Re: \r and \r\n does not work!

Thanks kimmo that's really helpful i never knew of the existence of the
<pre> tag.

Kimmo Laine wrote:
Quoted text here. Click to load it

Re: \r and \r\n does not work!

Quoted text here. Click to load it

If you are really generating a pure text file, forget about <pre> and just
specify Content-Type: text/plain instead of text/html.
- Tim Roberts, timr@probo.com
  Providenza & Boekelheide, Inc.

Re: \r and \r\n does not work!

For outputting plain text, I always do this:

header('Content-Type: text/plain');
// Send this header so that browsers won't treat it as HTML
echo "row1,row2,row3,row4\r\n"; // Use double quotes!
echo "row1,row2,row3,row4\r\n";
echo "row1,row2,row3,row4\r\n";
echo "row1,row2,row3,row4\r\n";
echo "row1,row2,row3,row4\r\n";
echo "row1,row2,row3,row4\r\n";

Site Timeline