Forcing Fputcsv to Use Enclosure for *All* Fields

Forcing fputcsv to Use Enclosure For *all* Fields

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.

php fputcsv and enclosing fields

Roll your own function - its not hard:

 function dumbcsv($file_handle, $data_array, $enclosure, $field_sep, $record_sep)
{
dumbescape(false, $enclosure);
$data_array=array_map('dumbescape',$data_array);
return fputs($file_handle,
$enclosure
. implode($enclosure . $field_sep . $enclosure, $data_array)
. $enclosure . $record_sep);
}
function dumbescape($in, $enclosure=false)
{
static $enc;
if ($enclosure===false) {
return str_replace($enc, '\\' . $enc, $in);
}
$enc=$enclosure;
}

(above is using unix style escaping)

C.

fputcsv inserting extra

Specify enclosure as blank which is the 4th optional parameter of fputcsv. Default enclosure is "

fputcsv($fp, $header,";", '');

Reference Link

Enclosing every field with double quotes in CSV file using PHP?

I have got solution
this function convert multi dimension array into CSV with double quote and

function arr_to_csv($arr)
{
$filePointer="export.csv";
$delimiter=",";
$enclosure='"';
$dataArray =$arr;

$string = "";

// No leading delimiter
$writeDelimiter = FALSE;
//foreach($dataArray as $dataElement)
foreach ($dataArray as $key1 => $value){
foreach ($value as $key => $dataElement)

{
// Replaces a double quote with two double quotes
$dataElement=str_replace("\"", "\"\"", $dataElement);
// Adds a delimiter before each field (except the first)
// Encloses each field with $enclosure and adds it to the string
if($writeDelimiter) $string .= $delimiter;
// $new_string = $enclosure . $dataElement . $enclosure;
$string .= $enclosure . $dataElement . $enclosure;
// Delimiters are used every time except the first.
$writeDelimiter = TRUE;
} // end foreach($dataArray as $dataElement)

$string .= "\n";

}
// Append new line
$string .= "\n";
//$string = "An infinite number of monkeys";
print($newstring);
// Write the string to the file
// fwrite($filePointer,$string);
header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename="'.$filePointer.'";');
}

Wrap CSV values generated by PHP fputcsv() with

This is not usually a problem for CSV files.

fputcsv puts quotes around the value if it would be ambiguous. For example,

a,b,"long string, with commas",,

is not ambiguous, but,

a,b,long string, with commas,,

is, and will in most (read: all) cases be interpreted by the CSV reader as having more than 5 fields.

CSV parsers will accept string literals even without quotes around them.

If you want quotes around the values anyway, the following snippet would do that. It doesn't escape quotes inside the string - that exercise is left to the reader:

$row = '"' . implode('", "', $rowitems) . '"';

You would want to put this in a loop for all your rows.



Related Topics



Leave a reply



Submit