0

I am trying to use php to write an array written in Javascript to a csv file with carriage return and line feeds separating the rows. The code below writes the csv file, but all on one line. Can anyone tell me how to alter it to include carriage return /line feed separatos between each line? Thanks

<?php
if(isset($_POST["Row"])){
    $rlist = $_POST["Row"];
    //$col = $_POST["Col"];
    $fname =($_POST["Fname"]);
    $fname = $fname . ".csv";
    $rArray = explode("-",$rlist);
    $totalR = count($rArray);
    $fp = fopen($fname, 'w');
    for($i =0; $i<$totalR;$i++){
        $row = $rArray[$i];
        $srow =explode(",",$row); 
        array_push($srow,"\n");
        fputcsv($fp,$srow);
    }
fclose($fp);
}
?>
IanR
  • 31
  • 1
  • 5
  • Are you opening the generated csv using excel? Maybe you need to use `\r\n` instead of `\n` – Felippe Duarte Oct 19 '18 at 16:16
  • `PHP_EOL` is a constant that creates a new line. Works as far as I know all the time. – Andreas Oct 19 '18 at 16:17
  • Possible duplicate of [Force new line in CSV generated file in PHP](https://stackoverflow.com/questions/14273801/force-new-line-in-csv-generated-file-in-php) – Andreas Oct 19 '18 at 16:21
  • `PHP_EOL` will return the _current_ system line ending. – Felippe Duarte Oct 19 '18 at 16:21
  • Thnk you for your replies - I have tried array_push($srow,"\r\n"); - that gives separate rows, but each row is enclosed in a single set of quotes - so the elements in the row create a single element. I have also tried array_push($srow,PHP_EOL), but that does not work either. – IanR Oct 19 '18 at 19:33

0 Answers0