I am having an array in PHP which contains some line breaks (\n
) also as the array elements. I am writing these array elements into the CSV file using fwrite
statement as below.
$newLine = $row[$i].",";
$newLine = "\n";
$csv2  = implode(" ", $newLine);
$file1 = fopen("/home/huadong/public_html/ramesh/output_updated.csv","w");
foreach ($csv2 as $line):
$line1 = ltrim($line);
fwrite($file1, $line1 . PHP_EOL);
ltrim is not working as it trims only leading white spaces. I have to replace the extra space in the beginning of the CSV file while it is getting written.
The CSV file is getting written as below.
I am expecting to write into the CSV file as below.