• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 937
  • 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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
-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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

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