473,382 Members | 1,784 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 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 34630
..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 (some text, right here), DDD I want this to come...
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 Netscape6 or Opera to view the same page the...
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 tags and French accented characters in the records....
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 file: <?xml-stylesheet href="test.xsl"...
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, _minit> But apparently, there is a problem with...
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 output is not including any cdata tags though?...
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 XslCompiledTransform to do the trnasform (namely the output...
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 and generate the HTML to be outputted. My problem...
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 addressed. Sorry for re-asking the question if it...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.