使用PHP将CSV文件中的双引号括起来
我需要使用
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; } } } 先谢谢你
在PHP中构建CSV时,您应该使用PHP 5中提供的
fputcsv函数
从文档中可以看到以下参数: int fputcsv ( resource $handle,array $fields [,string $delimiter = "," [,string $enclosure = '"' [,string $escape_char = "" ]]] ) 你有: >您正在写的文件资源 作为默认值,对于分隔符和“对于机箱,您不需要添加更多,这将正确地覆盖字符串.数字是一个不同的事情.有一个工作在this SO question,我发现后输入大部分内容,然后再出现数字问题:
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |