CSV in PHP. Lines not on one line

EDIT: Fixed, I had to remove all spaces I had (before the commas in the code below) and use trim

I'm trying to generate a CSV file using PHP. However the file splits into lines on its own. It looks fine in View Source, but excel/notepad show lines randomly broken up.

Here is my code :

// Echo Code Here
$string = '"REF1" , "FIRSTCLASS" ,"P" ,"1" ,"' . $orderref . '" ,"' . $fullname . '" ,"' . $add1 . '" ,"' . $add2 . '" ,"" ,"' . $postcode . '" ,"' . $city . '" ,"' . $country . '" ,"' . $fullname . '" ,"' . $telephone . '" ,"' . $email . '" ,"1" ,"1.0 kg"' . "\n";
echo $string;

Any help would be great, this is my first time working with CSV in PHP.

Upvotes: 0

Views: 175

Answers (2)

Míla Mrvík
Míla Mrvík

Reputation: 535

There is PHP function forwriting CSV into file:

PHP "fputcsv" function https://www.php.net/manual/en/function.fputcsv.php

Upvotes: 0

Fosco
Fosco

Reputation: 38506

Since you're using Windows, you should use Windows-style line endings: \r\n instead of just \n.

Upvotes: 2

Related Questions