I need to put all strings and number with double quotes in CSV file using PHP.
How can I create CSV file from PHP in all data within double quotes ?
I am using this code to generate CSV - I am using codeigniter framework
$array = array(
array(
(string)'XXX XX XX',
(string)'3',
(string)'68878353',
(string)'',
(string)'xxxx@xxxx.xxx.xx',
),
);
$this->load->helper('csv');
array_to_csv($array, 'blueform.csv');
Output I am getting:
"XXX XX XX",3,68878353,,xxxx@xxxx.xxx.xx
Expected Output:
"XXX XX XX","3","68878353","","xxxx@xxxx.xxx.xx"
Code of array_to_csv
if (!function_exists('array_to_csv')) {
function array_to_csv($array, $download = "") {
if ($download != "") {
header('Content-Type: application/csv');
header('Content-Disposition: attachement; filename="' . $download . '"');
}
ob_start();
$f = fopen('php://output', 'w') or show_error("Can't open php://output");
$n = 0;
foreach ($array as $line) {
$n++;
if (!fputcsv($f, $line)) {
show_error("Can't write line $n: $line");
}
}
fclose($f) or show_error("Can't close php://output");
$str = ob_get_contents();
ob_end_clean();
if ($download == "") {
return $str;
} else {
echo $str;
}
}
}
Thank you in advance