AdamShegrud Posted July 21, 2007 Share Posted July 21, 2007 Please be gentle, Ive just started with PHP 7 days ago. I am an expert at other subjects and know how it can be to answer simple questions like this on a board. But I truly am to much of a beginner to even search effectively, though I did try for a couple/few hours I haven't been able find an answer. Mostly due to the fact that every time i look in a post to find if my answer is in there I am interested and get sucked into what they are talking about. I can see why this site has its catch phrase now. Anywho, on with it. I have a site that takes some user input and a PHP that reads that data. The sole purpose of the PHP is to email that data to the admin. The full code is at the bottom of this post This snip is what Im having trouble with $body = ""; foreach($fields as $a => $b){ $body .= sprintf("%s%s",$_REQUEST[$a],$b); } Right now it says to take each item in the array above it and print it in the body of an email then follow that with a comma and continue to the next array item. In this manner the admin can take each email and copy the body to a text file then /n and copy the next emails body and so on then open the text file as a .csv. (I know this is an odd way to get the comma in there but the person who wrote it initially had the body of the email with inputbox_title: inputbox_data inputbox_title: inputbox_data inputbox_title: inputbox_data Since I didn't know much about it I just replaced all the Inputbox_Title stuff in the code with a comma and it works) Extra credit if you tell me a better way to do the comma thing but not necessary since that part of the code is working. However if the user puts any commas in any of the free form text fields thats going to mess up my .csv file. So my question is this is there a better solution to getting the input into a csv for the admin. And if not is there a way to search each string in the array for a comma and if it finds one replace it with a an apostrophe so as not to mess up the .csv formatting in the body of the email. <?php $to = "[email protected]" ; $from = $_REQUEST['Email_Address'] ; $name = $_REQUEST['Name'] ; $headers = "From: $from"; $subject = "Web Contact Data"; $fields = array(); $fields{"marital"} = ","; $fields{"how_did_you_hear_of_us"} = ","; $fields{"Name"} = ","; $fields{"sex"} = ","; $fields{"month"} = ","; $fields{"day"} = ","; $fields{"Birth_Year"} = ","; $fields{"Email_Address"} = ","; $fields{"Phone_Area_Code"} = ","; $fields{"Phone_Number"} = ","; $fields{"phone_number_type"} = ","; $fields{"alt_Area_Code"} = ","; $fields{"alt_Number"} = ","; $fields{"alt_number_type"} = ","; $fields{"terms"} = ","; $fields{"adventure1"} = ","; $fields{"adventure2"} = ","; $fields{"adventure3"} = ","; $fields{"adventure4"} = ","; $fields{"adventure5"} = ","; $fields{"adventure6"} = ","; $fields{"activities1"} = ","; $fields{"activities2"} = ","; $fields{"activities3"} = ","; $fields{"activities4"} = ","; $fields{"activities5"} = ","; $fields{"activities6"} = ","; $fields{"activities7"} = ","; $fields{"activities8"} = ","; $fields{"activities9"} = ","; $fields{"activities10"} = ","; $fields{"activities11"} = ","; $fields{"activities12"} = ","; $fields{"activities13"} = ","; $fields{"activities14"} = ","; $fields{"activities15"} = ","; $fields{"activities16"} = ","; $fields{"activities17"} = ","; $fields{"activities18"} = ","; $fields{"activities19"} = ","; $fields{"activities20"} = ","; $fields{"activities21"} = ","; $fields{"activities22"} = ","; $fields{"activities23"} = ","; $fields{"activities24"} = ","; $body = ""; foreach($fields as $a => $b){ $body .= sprintf("%s%s",$_REQUEST[$a],$b); } $headers2 = "From: [email protected]"; $subject2 = "Thank you for your enquiry"; $autoreply = "Thank you for your enquiry. You are now registered you for a free E-mail Newsletter. We have forwarded your info to the Adventure Group in your area. They will contact you shortly to answer any questions you may have and to tell you about their Adventure Group. Please be patient as they may be out whitewater rafting, wine tasting or at one of dozens of events they plan each month "; if($from == '') {print "You have not entered an email, please go back and try again";} else { if($name == '') {print "You have not entered a name, please go back and try again";} else { $send = mail($to, $subject, $body, $headers); if($send) {header( "Location: http://Site.com/confirm.htm" );} else {header( "Location: http://Site.com/confirm.htm" );} } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/61088-solved-replacing-a-with-a/ Share on other sites More sharing options...
wildteen88 Posted July 21, 2007 Share Posted July 21, 2007 Change your foreach loop to this: foreach($fields as $a => $b) { // Replace commas with a quote $_REQUEST[$a] = str_replace(',', '\'', $_REQUEST[$a]); $body .= sprintf("%s%s", $_REQUEST[$a], $b); } Quote Link to comment https://forums.phpfreaks.com/topic/61088-solved-replacing-a-with-a/#findComment-304042 Share on other sites More sharing options...
AdamShegrud Posted July 23, 2007 Author Share Posted July 23, 2007 @ Thanks alot Works perfectly. And in the search of how to fix this, I found info on the Mail::POP3Client module. Thats gonna let me get his data in a complete .csv file with just a double click. This is fun Quote Link to comment https://forums.phpfreaks.com/topic/61088-solved-replacing-a-with-a/#findComment-305108 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.