How to quote all values in fputcsv?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
esthermstrom
Forum Newbie
Posts: 9
Joined: Sat Apr 24, 2010 9:59 pm

How to quote all values in fputcsv?

Post by esthermstrom »

I have a piece of php code that reads values from a database resultset, shoves them into an array, and writes them to a CSV file. I need to force each value to be surrounded by double quotes when it goes into the CSV file, and can't for the life of me remember how to do this.

Here's the loop that writes to the file (this is actually contained in another loop to go through each row; this loop I'm showing goes through the columns in the currently-selected row):

Code: Select all

foreach ($row as $key => $val){
    $newrow[$key] = preg_replace("(\r\n|\n|\r|\t)", "", $val);
}

fputcsv($fp, $newrow);
What I need is for the value of $val to be enclosed in double quotes when it's written to the file. Right now, the results I get look like this:

Code: Select all

554,702180,25.00,6FYAK0,9090909090909090
What I need is this:

Code: Select all

"554","702180","25.00","6FYAK0","9090909090909090"
Can someone jump start my brain? I've already posted on stackoverflow, and basically all anyone could tell me was that fputcsv has a fourth parameter for string enclosure. The problem is, that only adds quotes for fields that are text-ish in the database. I have fields that are defined as dates, integers, etc., and all of them need to be quoted, because we're passing this data on to a legacy system that requires all import values to be in double quotes.

I've tried this:

Code: Select all

$newrow[$key] = '"' . preg_replace("(\r\n|\n|\r|\t)", "", $val) . '"';
but that returns this:

Code: Select all

"""554""","""702180""","""25.00""","""6FYAK0""","""9090909090909090"""
Is there really no way to do this?
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: How to quote all values in fputcsv?

Post by cpetercarter »

Have you tried casting each value as a string before putting it into $newrow?

Like this:

Code: Select all

foreach ($row as $key => $val){
    $t = preg_replace("(\r\n|\n|\r|\t)", "", $val);
    $newrow[$key] = (string)$t;
}

fputcsv($fp, $newrow);
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: How to quote all values in fputcsv?

Post by AbraCadaver »

Might work:

Code: Select all

fputcsv($fp, array_map('strval', $newrow), ',', '"');
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: How to quote all values in fputcsv?

Post by Weirdan »

fputcsv does not let you force the quoting. You need to use something different, like Luke's php-csv-utils: http://code.google.com/p/php-csv-utils/
Post Reply