Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 935
  • Last Modified:

How do I insert line breaks in a text file that is opened in a browser.

I am sure that this is a simple problem for the experts but I am really battling with this. I work on a web based system where clients can view time series data in certain formats, e.g. as a graph, table, csv, etc. When they select the csv format a file download dialog appears that asks if they want to open or save the file.

I also have to serve up the data in a specific text file format, say *.dhf. When the user selects this format the same file download dialog appears that asks if they want to open or save the file.  When they choose to open the file it is opened in notepad - which is correct!!

My problem is that I can not get the line breaks between the lines of data in the file. I build the file in php and I have tried "\r\n", "#13#10", "
", "
" but nothing seems to work. Please help.
0
RianaSteyn
Asked:
RianaSteyn
  • 3
  • 3
  • 2
  • +1
1 Solution
 
-null-Commented:
Do you have the code in question, as appending \r\n to the data output is correct, it's obviously just a small issue with the code logic.

-null-
0
 
ht-docsCommented:
Two ways:
1. Use <br> as line ends while showing your file in a browser
2. Show the content of your file inside <pre>..</pre> tag with normal \n endlines inside your file
0
 
Ionut A. TudorPHP ProgrammerCommented:
you could also try nl2br() function
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
-null-Commented:
I don't think they're outputting to a web page, they're writing to a file, and want the linebreaks to appear in notepad.

-null-
0
 
RianaSteynAuthor Commented:
Yes we are writing to a file and we want the linebreaks to appear in notepad when the file is opened. The correct data appears in notepad so the php logic that ads the data is correct. I add a "\r\n" after every line of data but I still don't have any linebreaks in notepad.
0
 
Ionut A. TudorPHP ProgrammerCommented:
I stand corrected, they are writing to a file that later a client will download and open. The \n in linux servers or \r\n in windows servers is ok, if you're writing from php you need to be sure the writing is correct:

correct - double qoutes
<?php
echo "\r\n\r\n";
?>

incorrect - single quotes
<?php
echo '\r\n\r\n';
?>
0
 
Ionut A. TudorPHP ProgrammerCommented:
It would be better to show us the part of your code that writes to file. Cheers
0
 
-null-Commented:
Can we see the line of code in question?  What you are doing sounds correct, there must just be a flaw in your logic.

-null-
0
 
RianaSteynAuthor Commented:
Thanks I think I previously had "\n\r" and not "\r\n". It seems to be working now.
0

Featured Post

Independent Software Vendors: 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!

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now