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
Need Response Body of Http 500 with File_Get_Contents (Php)
Passing Data via Modal Bootstrap and Getting PHP Variable
Opposite of Nl2Br? Is It Str_Replace
How to Parse Xml Containing Custom Namespaces Using Simplexml
How to Remove the Fatal Error When Fetching an Assoc Array
Laravel: I Can't Send More Then 2 Variables from Controller to a View
How to Implement a Bitmask in PHP
Guzzle 6: No More JSON() Method for Responses
How to Document Magic (_Call and _Callstatic) Methods for Ides
How to Detect Animated Gifs Using PHP and Gd
How to Fix 'Creating Default Object from Empty Value' Warning in PHP
Parse Math Operations with PHP
Php, Merging Arrays with Common Keys
What Is the Name for the "<<<" Operator
Cascade Dropdown List Using Jquery/Php