Is there a native function or solid class/library for writing an array as a line in a CSV file without enclosures? fputcsv
will default to "
if nothing is passed in for the enclosure param. Google is failing me (returning results for a whole bunch of pages about fputcsv
), and PEAR's libraries do more or less the same things as fputcsv
.
Something that works exactly like fputcsv
, but will allow the fields to remain unquoted.
currently: "field 1","field 2",field3hasNoSpaces
desired: field 1,field 2,field3hasNoSpaces
Doesn't this work?
I use tricky way to remove double quote, but only in Linux
This is what I use to put standard CSV into an array...
You can then output whatever you want in a foreach loop.
The downside with a CSV file with no enclosures means an errant comma in user input will munge the row. So you'll need to remove commas before writing a CSV row.
The tricky part with handling CSV is parsing enclosures, which makes the PHP & PEAR CSV functions valuable. Essentially you're looking for a file that is comma-delimited for columns and newline-delimited for rows. Here's a simple starting point:
Well
car(0)
didn't work out as theNULL
value will most likely choke most csv parsers.I ended using
fputcsv()
to build the initial file, then went through and removed all quotes. Elegant? Maybe not, but it got the job done :).