2

I got a coma separated string. I need to convert this to an array and be able to save it as a csv file.

$fp = fopen("abc.csv", "w");
$str = "FRUIT, STOCKS, TYPE\n lychee, instocks, B-type\n strawberry, N/A,A-type\n";

$data = str_getcsv($str);
fputcsv($fp, $data);
fclose($fp);

Currently it outputs the entire string as a single line.

Expected csv output:

FRUIT,       STOCKS,      TYPE
lychee,      instocks,    B-type
strawberry,  N/A,         A-type
5
  • "Currently it outputs the entire string as a single line." - then you need to use either <br> or <p> tags to output (line breaks) on screen. Commented Apr 5, 2016 at 12:47
  • @Fred-ii- Thanks for your comment. I'm currently saving it on the server as abc.csv. When I open the csv file, the entire string as a single line. I want the CSV to be as in my expected out put. Commented Apr 5, 2016 at 12:49
  • The string is already comma separated. Why not just write that string to the file? Commented Apr 5, 2016 at 12:54
  • @JayBlanchard Thank you. I'm not actually following you. What does write the string to the file mean? Commented Apr 5, 2016 at 12:57
  • 1
    @Becky You're welcome Becky. What Jay meant by that is as soon as you write that string to the file, the \n's will automatically create new lines for it. You just need to later call up that file to view it, rather than write to it. Commented Apr 5, 2016 at 13:31

4 Answers 4

3

You have two delimiters (newline and comma), so you want to run the function twice. Though as Jay pointed out, you may not need the foreach since it's already comma separated.

$rows = str_getcsv($str, PHP_EOL);
foreach($rows as $row) {
    $data = str_getcsv($row);
    fputcsv($fp, $data);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. Why is there a PHP_EOL?
PHP_EOL is a catchall end-of-line character. Linux and Windows use different characters (\n vs \r\n), and PHP will detect which line ending to use. Or you can use "\n" instead of PHP_EOL.
2

For your exact string you would need to do this:

file_put_contents('abc.csv', "FRUIT, STOCKS, TYPE\n lychee, instocks, B-type\n strawberry, N/A,A-type\n");

The reason that your current code is failing is because the fputcsv() function is taking your values and double-quoting them as necessary.

Open your CSV file with a text editor and not a spreadsheet viewer and you will see what I am talking about.

Comments

2

You can first of all use the break line delimiter to separate each row and then, using the comma delimited to separate each value of your column as you can see at the example.

Is very important to use the double quote in the break line delimiter ("\n") when you use any php function.

I recommend use the trim function to delete white spaces.

$fp = fopen("abc.csv", "w");
$str = "FRUIT, STOCKS, TYPE\n lychee, instocks, B-type\n strawberry, N/A,A-type\n";

$data = str_getcsv($str,"\n");

foreach($data as $line){
    fputcsv($fp, str_getcsv($trim($line)));
}

fclose($fp);

Regards!

1 Comment

Why should the OP "try this"? A good answer will always have an explanation of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO.
0

Try

$fp = fopen("abc.csv", "w");
$array= array("FRUIT,STOCKS,TYPE","lychee,instocks,B-type","strawberry, N/A,A-type"); 
foreach ($array as $row) {               
    fwrite($fp, $row . "\r\n");
}
fclose($fp);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.