tags:

views:

51

answers:

4

I'm generating a csv file using php, now some columns contain a paragraph with commas, now when I open the file , every comma within the file counts as a new column, is it maybe possible to escape these commas on a way?

+4  A: 

Depends, what, your, CSV, reader, is, "but, quoting, should, work"

dhill
+1  A: 

That's the BIG problem with using a , in a CSV file. I would recommend using a different separator like | (it's less likely to appear on a text) or using a different more robust file format like XML for generating your file.

Pablo Santa Cruz
+1  A: 

Many CSV readers will allow commas within a single column by surrounding the column with double quotes. In that case, double quotes can be represented by double double quotes:

column 1,"column 2, with comma","column 3 with ""quote chars"", and comma"
paxdiablo
A: 

You're using a comma because it's a delimiter. That is, the comma has special meaning no matter when its used. By that very definition, it becomes hard to treat it as context sensitive. It can be done though, considering symbols like '\n.

You can try a new delimiter, such as ,\n, though that might not be an option for you. Looks to me that the best solution would be to use a different persistence mechanism. Things will get sticky otherwise.

Mike