我需要使用
PHP将CSV文件中的所有字符串和数字双引号括起来.
如何在双引号内的所有数据中从PHP创建CSV文件?
我正在使用此代码生成CSV – 我正在使用codeigniter框架
$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');
输出我得到:
"XXX XX XX",3,68878353,,xxxx@xxxx.xxx.xx
预期产量:
"XXX XX XX","3","68878353","","xxxx@xxxx.xxx.xx"
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;
}
}
}
先谢谢你