PHP form that generates CSV – removing commas from the comments field


I have a dual purpose HTML form which return both email and a CSV file to my server.

On my HTML form I have a field titled ‘Notes’ which is a text area.
I need to prevent the CSV output file from seeing any commas a user may enter into the text area.

This avoids the ‘Notes’ area of the CSV output file from spanning multiple cells accidentally.

Ideally I would like a code that prevents both commas and returns, but I’ll settle for commas. Please be sure to indicate WHERE your solution should go in my code below. Im newer to PHP and thus don’t understand order.

Here is my PHP code:


    //--------------------------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 = '[email protected]';

    // create email headers

    // Where to redirect after form is processed.  
    $url = ''; 

    // 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";

        $j = strlen($key);

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

        $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);



//example data
$_POST['Email']="[email protected]";
$_POST['ReferredBy']="[email protected]";
$_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);

the csv file will look like:

kraysak,[email protected],kraysakland,ky,98756,[email protected],"id like to help, but i dont know how"

this can help you, but, i realize that you don’t know how to do to edit your code, so…

change the

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


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

next, replace the if/else sentece by

if (file_exists($filename)) {
    $file = fopen($filename, 'w');
    fputcsv($file, $string );
} else {
    $file = fopen($filename, 'w'); 
    fputcsv($file, $string );

i hope this can help you, and in the case, you can make the necessary corrections

Answered By – kraysak

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published