PHP - Webform到csv(从字段中分割逗号(不想要))

时间:2017-11-30 08:01:56

标签: php csv

我的网站上有一些PHP'我用来将一些数据从webform发送到csv文件。

它正在运行,但名为'primary_classification'的字段包含一些带逗号的文本。通过这些分类,它将文本拆分为csv中的两个单元格。

这是PHP:

<?php
    $keys = array('user_name',  'gr_num', 'primary_classification');
    $csv_line = array();
    foreach($keys as $key){
        array_push($csv_line,'' . $_GET[$key]);
    }
    $fname = 'test.csv';
    $csv_line = implode(',',$csv_line);
    if(!file_exists($fname)){$csv_line = "\r\n" . $csv_line;}
    $fcon = fopen($fname,'a');
    $fcontent = $csv_line;
    fwrite($fcon,$csv_line."\n");
    fclose($fcon);

    echo ("<SCRIPT LANGUAGE='JavaScript'>
    window.alert('The classification has been added!')
    window.location.href='http://localhost:8080/classification.html';
    </SCRIPT>");

?>

这是使用&#39;个人行为&#39;时csv的输出。 (工作),以及&#39;教育,学习和技能&#39; (不工作)。

enter image description here

有谁知道我哪里出错了?

由于

1 个答案:

答案 0 :(得分:1)

我很确定如果用引号括起你的字段就足够了,所以只需更改:

array_push($csv_line,'' . $_GET[$key]);

进入

array_push($csv_line, '"' . $_GET[$key] . '"');