Help Needed with CSV File Formatting

247SiteAlert's picture

They have: 18 posts

Joined: Oct 2007

My .php script is exporting some data to a .csv file. The problem I'm having is displaying data that has multiple lines in it. To keep the linefeeds and CRs from screwing up the .csv file, I am presently replacing LF and CRs with [lf] and [cr]. I can now see these in the csv file when I open it in Excel.

I can't find a way to either leave one CR in or LF out (or any combination) so that when I open in Excel the data is on two lines in the same cell.

Presently my data in one cell looks like this:

This is a[cr][lf] test of[cr][lf] multiple lines.

And I want it to end up displaying like this in one cell:

This is a
test of
multiple lines.

Thanks for any help.

Free Website Uptime Monitoring
http://www.247SiteAlert.com

Greg K's picture

He has: 2,145 posts

Joined: Nov 2003

I'm not sure if it can be done. The only thing I can think of is try running phpMyAdmin, create a sample table, then go to EXPORT and choose Excel format, and instead of telling excel to open the file, tell it to open in notepad to see how that formats them....

If I think about it tomorrow and get time, I'll play around with it and se what I can come up with.

-Greg

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

Putting the fields in quotes should do the trick.

Here is a sample:

"this
is
a
test","hello"
"how are
you?","I am
fine"
'

Here is the result:

carloncho's picture

They have: 17 posts

Joined: Jan 2008

How you write the csv file? with fputcsv function?

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

I never knew that function existed. When I need to output a csv-formatted file, I just echo everything as plain text and format it manually.

He has: 629 posts

Joined: May 2007

A couple of things to bear in mind with CSV files:

  • Any field containing a comma also needs quotes around it
  • Double any quotes that may occur inside such a quoted field

But you probably already knew that.

Cordially, David
--
delete from internet where user_agent="MSIE" and version < 8;

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.