单个csv文件列中的多个逗号值

时间:2017-04-01 11:12:28

标签: php csv

我没有在单列中获得多个单独的值。感谢您的帮助

$fname = "vijay";
$mname = "a,b";
$csvData = "";
   $csvData .= "First Name";
    $csvData .= ",Middle Name";
    $csvData .= "\n\r";
    $csvData .= "$fname" . ",";
    $csvData .= "$mname";
    $csvData .= "\n\r";
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: text/csv");
header("Content-Disposition: attachment; filename=student-report-data-" . date("dMY_H:i") . ".csv");
header('Pragma: no-cache');
header('Expires: 0');
echo $csvData;
exit;

enter image description here

1 个答案:

答案 0 :(得分:0)

尝试将它们包装在引号内。您的第二个输入,正在创建一个新列,而不是按照您希望的方式行事,保持内联。我建议调查http://php.net/manual/en/function.fputcsv.php以减少劳动密集度。

$fname = "vijay";
$mname = "a,b";
$csvData = "";
     $csvData .= "\"First Name\"";
        $csvData .= ",\"Middle Name\"";
        $csvData .= "\n\r";
        $csvData .= "\"$fname\"" . ",";
        $csvData .= "\"$mname\"";
        $csvData .= "\n\r";
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: text/csv");
header("Content-Disposition: attachment; filename=student-report-data-" . date("dMY_H:i") . ".csv");
header('Pragma: no-cache');
header('Expires: 0');
echo $csvData;
exit;   

如果您的字符串中有引号,则必须在\之前添加",只需要抬头。