Forum Moderators: coopster

Message Too Old, No Replies

Inserting quotes in CSV file

How to insert quotes

         

Ice99

3:07 pm on Sep 6, 2007 (gmt 0)

10+ Year Member



Newbie here - trying to figure out how to include quotes around each field inserted in a csv file (e.g. "Name", "Address"). I've tried what I would do in an echo statement (e.g. escaping and adding quotes - (e.g. $name = /"name"/;) but it inserts an extra set of quotes around it so it's now """Name""".

$list = array
(
"$company,$fname $lname,$fname,$title,$email,$address1,$address2,$city,$state,$zip,$phone,$fax",
);

$file = fopen("contacts.csv","w");

foreach ($list as $line)
{
fputcsv($file,split(',',$line));
}
fclose($file);

Any help would be so greatly appreciated.

dreamcatcher

3:10 pm on Sep 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi ice99,

A warm welcome to WebmasterWorld. :)

Try this:
fputcsv($file,split(',','"$line"'));

or in your array:

$list = array
(
'"$company","$fname $lname","$fname","$title","$email","$address1","$address2","$city","$state","$zip","$phone","$fax"',
);

dc

Ice99

4:36 pm on Sep 6, 2007 (gmt 0)

10+ Year Member



Thanks for the suggestion - I tried both - the first (putting '" around $list I end up with '$File' in one column of the CSV.

The second (one the array itself) - I end up with everyting in one column with '/' escape characters - no quotes.

I've even tried doing the '"$name"' around each variable but it still puts 3 quotes in the csv (if you view in notepad) (e.g. """John""").

And the only thing that works with the quotes is when there's a space in the form field (e.g. "123 street").

Totally stumped here...

I know the default for the enclosure is the double quote - any other suggestions?