472,353 Members | 1,902 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

escaping commas for csv output

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
9 34421
..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
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
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
..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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: felciano | last post by:
Hi -- I am trying to use the csv module to parse a column of values containing comma-delimited values with unusual escaping: AAA, BBB, CCC...
6
by: Joe Price | last post by:
Hi all I've formatted one of my xml files for viewing through a web browser using xsl. It works fine in Internet Explorer, however when I use...
1
by: Lisa | last post by:
I need to apply the HTML formatting tags and the French accented characters in a XML document. The XML is generated from a database that has HTML...
5
by: Troot | last post by:
Hi All, I was wondering if someone could clear this up for me. I have constructed a sample for a bigger problem I'm having. So, given the xml...
8
by: Frank Rizzo | last post by:
Hello, I'd like to have the following structure in my XML file <lname, _fname, _minit> <status>it is all good</status> </lname, _fname,...
4
by: werD | last post by:
Hello, Ive been reading about cdata-section-elements b/c ive got a comments field that id like to inlcude some html in. For some reason the final...
4
by: Jon | last post by:
Hi, I used XslCompiledTransform with the following Xsl file. The <xsl:text disable-output-escaping="yes"does not work when using...
5
by: eva.mukhija | last post by:
Hi I need to insert some html content generated by certain business logic into an xsl output. I have been able to insert custom Java extensions...
1
by: David Henderson | last post by:
I know 'disable-output-escaping' has been discussed in the past, but I can't put my finger on any of the threads to see if my current problem is...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.