Reputation: 175
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
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