4

I've got a form which submits data to a csv file. When a user inputs a comma to a field, it destroys my csv structure. I want to convert inputted commas so that they can get displayed as a character.

I tried this:

$_POST["field"] = str_replace(",", "','", $_POST["field"]);
5
  • What are you using to generate the CSV file? Commented Sep 29, 2015 at 8:35
  • use phpExcel to produce result .. Commented Sep 29, 2015 at 8:36
  • 1
    The correct CSV structure is that each column should be wrapped with double quotes, and then any double quotes inside to be escaped. Commented Sep 29, 2015 at 8:36
  • 1
    Instead of writing your own escaping and formatting mechanism, consider using fputcsv. Commented Sep 29, 2015 at 8:38
  • phpExcel is depricated for now ! see github.com/PHPOffice/PHPExcel below is right answer "use fputcsv() to write, and fgetcsv() to read the file" its a safe and correct way ! versus just replace POST, that can overflowed your serv Commented Dec 31, 2018 at 11:18

4 Answers 4

5

Use html encoding for instant relief , but still my recommendation to use phpExcel

$comma="&#44";
$_POST["field"] = str_replace(",", $comma, $_POST["field"]);
Sign up to request clarification or add additional context in comments.

5 Comments

Why on earth would you recommend a whole other library, when PHP supports CSV out of the box with fputcsv and fgetcsv? Also, if you're going to encode one thing, why not encode the entire string properly instead of just hacking in a single HTML entity?
This worked well, thanks! If you don't want to use fputcsv() this solution is very good.
@JonStirling , CSV stands for comma seperated values. You can't achieve extra functionalites because CSV file doesn't support font styles etc .. so i recommended not emphasized .. overall , thanks for your useful advice.
thanx, correct answer! Cause not all *.csv imports clearly understand where right commas !!! for example: google feeds(except google docs )) ) can't uderstend correct commas but libreOffice, Excel - understand csv file right !
Man!! you saved me from a lot of headache <3
4

You can use fputcsv() to write, and fgetcsv() to read the file, it automatically converts your string.

A simple example for writing the data:

$csv = fopen('file.csv', 'w');
$array = array($csv);
fputcsv($csv, $array);

And reading the data:

$csv = fopen('file.csv','r');
print_r(fgetcsv($csv));

2 Comments

+1 because imo this is the right way to do it, but providing an example would go a long way to making it a "good" answer.
fputcsv takes an array as the second param.
0

You can escape coma like this:

$_POST["field"] = str_replace(",", "\,", $_POST["field"]);

Or you can put string in quotes

$_POST["field"] = "'".$_POST["field"]."'";

Comments

-1

Probably not the best answer, but it does work.

You could replace the comma with a random string when inputting to the CSV as below:

$commastring = str_replace(",", "/zwdz/", $tempstring);

and then when you need to output the comma somewhere on your website (if a database website) you can do the opposite str_replace

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.