wiseman7687
wiseman7687

Reputation: 175

Linebreaks do not occur - PHP to CSV

I have a script that copys results from a table and saves it in a csv format. Everything is running fine with the script except that it does not recognize \n and just inputs it in the last cell like blahblah\r\n.

I have these lines currently but neither situation is creating the new line...-

$csv_output .= "$lastName,$firstName,$agentMLSID,$agentBoard,$directOffice,$directOfficeExtension,$voicemail,$mobile,$email,$officeName ".PHP_EOL;


$csv_hdr = "lastname,firstname,mslid,board,directline,ext,vm,mobile,email,office";
$csv_hdr .= "\n";

Upvotes: 0

Views: 101

Answers (1)

Matthias Robbers
Matthias Robbers

Reputation: 15728

fputcsv() is what you need.

<?php

$list = array (
   array('aaa', 'bbb', 'ccc', 'dddd'),
   array('123', '456', '789')
);

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

foreach ($list as $fields) {
   fputcsv($fp, $fields);
}

fclose($fp);

?>

Output (file.csv):

aaa,bbb,ccc,dddd
123,456,789

Upvotes: 1

Related Questions