生成CSV的PHP表单 - 从评论字段中删除逗号

时间:2014-01-28 19:57:27

标签: php csv character comma

我有一个双重用途的HTML表单,它将电子邮件和CSV文件都返回给我的服务器。

在我的HTML表单上,我有一个标题为'Notes'的字段,它是一个文本区域。 我需要阻止CSV输出文件看到用户可能进入文本区域的任何逗号。

这可以避免CSV输出文件的“注释”区域意外地跨越多个单元格。

理想情况下,我想要一个可以阻止逗号和返回的代码,但我会满足于逗号。请务必在下面的代码中指出您的解决方案应该放在哪里。我对PHP更新,因此不理解顺序。

这是我的PHP代码:

<?php

    //--------------------------Set these paramaters--------------------------

    // Subject of email sent to you.
    $subject = 'Atlas Race Volunteer Inquiry';  

    // Your email address. This is where the form information will be sent.         
    $emailadd = 'atlas@atlasrace.com';

    // create email headers

    // Where to redirect after form is processed.  
    $url = 'http://www.atlasrace.com/thankyou4.html'; 

    // Makes all fields required. If set to '1' no field can not be empty. If set to '0' any                or all fields can be empty.
    $req = '0'; 

    // --------------------------Do not edit below this line--------------------------
    $text = "Results from form:\n\n";       
    $space = '  ';
    $line = '
     ';

    foreach ($_POST as $key => $value) {
        if ($req == '1'){
            if ($value == ''){
                echo "$key is empty";
                die;
            }
        }

        $j = strlen($key);

        if ($j >= 20){
            echo "Name of form element $key cannot be longer than 20 characters";
            die;
        }

        $j = 20 - $j;

        for ($i = 1; $i <= $j; $i++){
            $space .= ' ';
        }

        $value = str_replace('\n', "$line", $value);
        $conc = "{$key}:$space{$value}$line";
        $text .= $conc;
        $space = '  ';

    }

    mail($emailadd, $subject, $text, 'Reply-To: '.$mail.'', 'From: '.$emailadd.'');

    echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';


    $filename = "volunteerdata.csv";
    $string = $_POST['Name'].",".$_POST['Email'].",".$_POST['City'].",".$_POST['State'].",".$_POST['Phone'].",".$_POST['ReferredBy'].",".$_POST['Notes']."\n";

    if (file_exists($filename)) {
        $file = fopen($filename, "a");
        fwrite($file, $string);
    } else {
        $file = fopen($filename, "a");
        fwrite($file, '"Name","Email","City","State","Phone","ReferredBy","Notes",\n');
        fwrite($file, $string);
    }

    fclose($file);
?>

2 个答案:

答案 0 :(得分:0)

//example data
$_POST['Name']="kraysak";
$_POST['Email']="k@kraysak.gov";
$_POST['City']="kraysakland";
$_POST['State']="ky";
$_POST['Phone']="98756";
$_POST['ReferredBy']="kraysak@k.gov";
$_POST['Notes']="id like to help, but i dont know how";

$foo = array($_POST['Name'],$_POST['Email'],$_POST['City'],$_POST['State'],$_POST['Phone'],$_POST['ReferredBy'],$_POST['Notes']);  
$fp = fopen($filename, 'w');
 fputcsv($fp, $foo);
fclose($fp);

csv文件如下所示:

kraysak,k@kraysak.gov,kraysakland,ky,98756,kraysak@k.gov,"id like to help, but i dont know how"

这可以帮助你,但是,我意识到你不知道如何编辑代码,所以...

更改

$string = $_POST['Name'].",".$_POST['Email'].","... etc

通过

$string = array($_POST['Name'],$_POST['Email'],$_POST['City'],$_POST['State'],$_POST['Phone'],$_POST['ReferredBy'],$_POST['Notes']);  

接下来,用

替换if / else sentece
if (file_exists($filename)) {
    $file = fopen($filename, 'w');
    fputcsv($file, $string );
} else {
    $file = fopen($filename, 'w'); 
    $head_data=array("Name","Email","City","State","Phone","ReferredBy","Notes");
    fputcsv($file,$head_data);
    fputcsv($file, $string );
}

我希望这可以帮到你,在这种情况下,你可以进行必要的修正

答案 1 :(得分:0)

我不得不制作一个轻微的mod,因为输出的CSV文件覆盖了第一行中带有用户数据的标签。我的mod很轻微:

if (file_exists($filename)) {
$file = fopen($filename, 'a');
fputcsv($file, $string );
} else {
$file = fopen($filename, 'a'); 
$head_data=array("Name","Email","City","State","Phone","ReferredBy","Notes");
fputcsv($file,$head_data);
fputcsv($file, $string );
}

将$ file语句中的“w”更改为“a”