Forcing fputcsv to Use Enclosure For *all* Fields

43,263

Solution 1

No, fputcsv() only encloses the field under the following conditions

/* enclose a field that contains a delimiter, an enclosure character, or a newline */
if (FPUTCSV_FLD_CHK(delimiter) ||
  FPUTCSV_FLD_CHK(enclosure) ||
  FPUTCSV_FLD_CHK(escape_char) ||
  FPUTCSV_FLD_CHK('\n') ||
  FPUTCSV_FLD_CHK('\r') ||
  FPUTCSV_FLD_CHK('\t') ||
  FPUTCSV_FLD_CHK(' ')
)

There is no "always enclose" option.

Solution 2

Not happy with this solution but it is what I did and worked. The idea is to set an empty char as enclosure character on fputcsv and add some quotes on every element of your array.

function encodeFunc($value) {
    return "\"$value\"";
}

fputcsv($handler, array_map(encodeFunc, $array), ',', chr(0));

Solution 3

Building on Martin's answer, if you want to avoid inserting any characters that don't stem from the source array (Chr(127), Chr(0), etc), you can replace the fputcsv() line with the following instead:

fputs($fp, implode(",", array_map("encodeFunc", $row))."\r\n");

Granted, fputs() is slower than fputcsv(), but it's a cleaner output. The complete code is thus:

/***
 * @param $value array
 * @return string array values enclosed in quotes every time.
 */
function encodeFunc($value) {
    ///remove any ESCAPED double quotes within string.
    $value = str_replace('\\"','"',$value);
    //then force escape these same double quotes And Any UNESCAPED Ones.
    $value = str_replace('"','\"',$value);
    //force wrap value in quotes and return
    return '"'.$value.'"';
}

$fp = fopen("filename.csv", 'w');
foreach($table as $row){
    fputs($fp, implode(",", array_map("encodeFunc", $row))."\r\n");
}
fclose($fp);

Solution 4

After a lot of scrafffing around and some somewhat tedious character checking, I have a version of the above referenced codes by Diego and Mahn that will correctly strip out encasings and replace with double quotes on all fields in fputcsv. and then output the file to the browser to download.

I also had a secondary issue of not being able to be sure that double quotes were always / never escaped.

Specifically for when outputting directly to browser using the php://input stream as referenced by Diego. Chr(127) is a space character so the CSV file has a few more spaces than otherwise but I believe this sidesteps the issue of chr(0) NULL characters in UTF-8.

/***
 * @param $value array
 * @return string array values enclosed in quotes every time.
 */
function encodeFunc($value) {
    ///remove any ESCAPED double quotes within string.
    $value = str_replace('\\"','"',$value);
    //then force escape these same double quotes And Any UNESCAPED Ones.
    $value = str_replace('"','\"',$value);
    //force wrap value in quotes and return
    return '"'.$value.'"';
}


$result = $array_Set_Of_DataBase_Results;
$fp = fopen('php://output', 'w');
if ($fp && $result) {
    header('Content-Type: text/csv');
    header('Content-Disposition: attachment; filename="export-'.date("d-m-Y").'.csv"');
    foreach($result as $row) {
        fputcsv($fp, array_map("encodeFunc", $row), ',', chr(127));
    }
    unset($result,$row);
    die;
}

I hope this is useful for some one.

Share:
43,263

Related videos on Youtube

Alan Storm
Author by

Alan Storm

Portland based Web Developer/Programmer/Engineer. Projects include No Frills Magento Layout, the only Magento layout book you'll ever need and Commerce Bug, the debugging extension for the Magento Ecommerce system. If you're interested in low cost, in-depth mentoring/tutoring, checkout my Patreon campaign.

Updated on November 12, 2020

Comments

  • Alan Storm
    Alan Storm over 3 years

    When I use fputcsv to write out a line to an open file handle, PHP will add an enclosing character to any column that it believes needs it, but will leave other columns without the enclosures.

    For example, you might end up with a line like this

    11,"Bob ",Jenkins,"200 main st. USA ",etc
    

    Short of appending a bogus space to the end of every field, is there any way to force fputcsv to always enclose columns with the enclosure (defaults to a ") character?

    • pspahn
      pspahn over 9 years
      Just curious, as my guess is that you're question was in:re to Magento/Varien_Io_File::streamWriteCsv() (which ultimately just uses fputcsv), did you ever find a good solution for this? Possibly using Varien_File_Csv?
    • Alan Storm
      Alan Storm over 9 years
      @pspahn Never did, (and this was four+ years ago, but I don't think it was specifically Magento related)
    • Felix Geenen
      Felix Geenen about 3 years
      Take a look at this custom fputcsv implementation that should fit your needs: stackoverflow.com/a/66682050/592868
  • nalply
    nalply over 13 years
    sad, because fgetcsv fails with Österreich without double quotes!
  • ftrotter
    ftrotter about 12 years
    VolkerK is, of course quite right. fputcsv cannot help you with this problem (I have the same issue). Let me help you get a jump start on the next search you will be doing: http://stackoverflow.com/questions/3933668/convert-array-int‌​o-csv
  • Tom B
    Tom B almost 10 years
    Including chr(0) in the file causes issues in some import scripts if the file is UTF-8
  • Mike Castro Demaria
    Mike Castro Demaria about 9 years
    the csv contain ^@ if you are using chr(0)
  • Mahn
    Mahn over 8 years
    You can get rid of the nulls (chr(0)) in the resulting file by filtering them after you are done outputing your csv: file_put_contents($file, str_replace(chr(0), '', file_get_contents($file)));. Also your encodeFunc will need to escape double quotes as well if your values have them. See my revision here. Other than that it seems to work, just keep in mind it's a hack.
  • Martin
    Martin over 7 years
    I did actually find after posting this answer that with fputcsv that the function really is pretty lame and using standard write-to-file functions as you've also done sorted my code out and sidestepped all issues with CSV consistency across platforms and programs.
  • Mike Finch
    Mike Finch almost 7 years
    Thank you for the good example. I needed to always enclose all values in quotes. And, I needed to force the line endings to CR+LF instead of just LF. This example solves both problems.
  • relipse
    relipse almost 4 years
    where did you find this? What is your source?
  • CollectiveWin
    CollectiveWin almost 4 years
    @relipse It's from the php source code, in /ext/standard/file.c php_fputcsv