By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,304 Members | 1,254 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,304 IT Pros & Developers. It's quick & easy.

escaping commas for csv output

P: n/a
I'm outputting form content into a csv file. If a comma is used in one
of the fields, however, it will interpret to go to next column. Is there
a workaround? Thanks.

$fp = fopen('my.csv','a');
$content = "$var1,$var2,$var3...
fwrite($fp,$content);
Sep 24 '08 #1
Share this Question
Share on Google+
9 Replies


P: n/a
..oO(Bruce)
>I'm outputting form content into a csv file. If a comma is used in one
of the fields, however, it will interpret to go to next column. Is there
a workaround? Thanks.

$fp = fopen('my.csv','a');
$content = "$var1,$var2,$var3...
fwrite($fp,$content);
fputcsv()

Micha
Sep 24 '08 #2

P: n/a
Bruce wrote:
I'm outputting form content into a csv file. If a comma is used in one
of the fields, however, it will interpret to go to next column.
You should use fputcsv().

Alternatively, there are two typical solutions for this:
1) use another delimiter which is less likely to occur in the data
2) escape the delimiter

Of course, you need both.

Solution 1: Consider for example that every var is quoted like this:
"banana", "apple", "pear"
Now, the delimiter has become ", ", and there is a " at the beginning and
the end. However, if the string ", " occurs in the data, you still have a
problem.

Solution 2: Consider that a comma may occur in the data. You have to
distinguish between a comma in the data and a comma at the end of the
data. This is done by preceding the comma with an escape character:
banana, apple, pear, hello\, I said
Here, the comma in the data is preceded by a backslash, to indicate that
it belongs to the data. But now, if you want to put \, in the data, it
will remove the backslash. Therefore, you also escape the backslash, like
so: \\,

A backslash is a typical escape character, but fputcsv() uses quotes to
escape quotes, so "" becomes ":
"banana", "apple", """Hello"", I said"
Sep 24 '08 #3

P: n/a
Michael Fesser wrote:
>>
$fp = fopen('my.csv','a');
$content = "$var1,$var2,$var3...
fwrite($fp,$content);

fputcsv()

Micha
I'm assuming replacing fwrite() with fputcsv().

Was there something else (it did not work)?
Sep 24 '08 #4

P: n/a
..oO(Bruce)
>Michael Fesser wrote:
>>>
$fp = fopen('my.csv','a');
$content = "$var1,$var2,$var3...
fwrite($fp,$content);

fputcsv()

Micha

I'm assuming replacing fwrite() with fputcsv().
Not exactly.
>Was there something else (it did not work)?
Sometimes it helps to have a look at the manual. There's even an
example.

Micha
Sep 24 '08 #5

P: n/a
On Sep 24, 7:46*pm, Bruce <n...@none.netwrote:
I'm outputting form content into a csv file. If a comma is used in one
of the fields, however, it will interpret to go to next column. Is there
a workaround? Thanks.

$fp = fopen('my.csv','a');
$content = "$var1,$var2,$var3...
fwrite($fp,$content);
If your aim is to write out data that would be compatible with
spreadsheet software, then the standard solution is to enclose any
field that's a string in quotes. For example, if your data was:

1,2,3,a,b,c,a man, a plan, a canal, panama

then rendering it as follows should produce the correct results when
you import into your CSV supporting spreadsheet application:

"1","2","3","a","b","c","a man, a plan, a canal, panama"

Of course, you then hit the problem of what to do if your string
contains a " character?

1,2,3,a,b,c,a man, a plan, a canal, "panama"

The answer is that you can escape the " character by doubling it up in
the output.

"1","2","3","a","b","c","a man, a plan, a canal, ""panama""

Data formatted like this should be imported correctly into most
software that supports with CSV. Do bear in mind, that Microsoft
Excel can have problems if any of your data contains a newline (the
general consensus is that if the newline is contained within quotes it
should be treated as part of the field instead of a line terminator,
but Excel seems to ignore that).

http://en.wikipedia.org/wiki/Comma-separated_values
Sep 25 '08 #6

P: n/a
On 24 Sep, 19:46, Bruce <n...@none.netwrote:
I'm outputting form content into a csv file. If a comma is used in one
of the fields, however, it will interpret to go to next column. Is there
a workaround? Thanks.

$fp = fopen('my.csv','a');
$content = "$var1,$var2,$var3...
fwrite($fp,$content);
If you're planning on using the output for Excel, I would recommend
using tabs instead of commas to delimit the file.
Sep 25 '08 #7

P: n/a
Michael Fesser wrote:
.oO(Bruce)
>Michael Fesser wrote:
>>>$fp = fopen('my.csv','a');
$content = "$var1,$var2,$var3...
fwrite($fp,$content);
fputcsv()

Micha
I'm assuming replacing fwrite() with fputcsv().

Not exactly.
>Was there something else (it did not work)?

Sometimes it helps to have a look at the manual. There's even an
example.
Instead of writing to a file what if we just wanted to "write" to a
variable, and later write a header and echo that variable. Is there a
php method of doing that (I couldn't find it). Or should I just roll my
own, I was going to do that (doesn't seem like much) until I read this
thread...

Jeff
>
Micha
Sep 27 '08 #8

P: n/a
On 27 Sep, 14:10, Jeff <jeff@spam_me_not.comwrote:
Michael Fesser wrote:
.oO(Bruce)
Michael Fesser wrote:
$fp = fopen('my.csv','a');
$content = "$var1,$var2,$var3...
fwrite($fp,$content);
fputcsv()
>Micha
I'm assuming replacing fwrite() with fputcsv().
Not exactly.
Was there something else (it did not work)?
Sometimes it helps to have a look at the manual. There's even an
example.

Instead of writing to a file what if we just wanted to "write" to a
variable, and later write a header and echo that variable. Is there a
php method of doing that (I couldn't find it).
There are many examples of this!
Sep 27 '08 #9

P: n/a
Captain Paralytic wrote:
On 27 Sep, 14:10, Jeff <jeff@spam_me_not.comwrote:
>Michael Fesser wrote:
>>.oO(Bruce)
Michael Fesser wrote:
>$fp = fopen('my.csv','a');
>$content = "$var1,$var2,$var3...
>fwrite($fp,$content);
fputcsv()
Micha
I'm assuming replacing fwrite() with fputcsv().
Not exactly.
Was there something else (it did not work)?
Sometimes it helps to have a look at the manual. There's even an
example.
Instead of writing to a file what if we just wanted to "write" to a
variable, and later write a header and echo that variable. Is there a
php method of doing that (I couldn't find it).
There are many examples of this!
Well, I found this:
$csv = fopen('php://temp/maxmemory:'. (5*1024*1024), 'r+');

fputcsv($csv, array('blah','blah'));

rewind($csv);

// put it all in a variable
$output = stream_get_contents($csv);

But frankly, I don't understand why there isn't a more direct method.

function csvLine($array){
$line = array();

foreach($array as $item){
$item = str_replace('"','""',$item);
$line[] = '"' . $item . '"';
}

$content = implode(',',$line);

return $content . "\n";
}

I don't believe Excell would have any trouble opening that.

Have I missed something?

Jeff
Sep 27 '08 #10

This discussion thread is closed

Replies have been disabled for this discussion.