0

I am trying to replace empty field with \N in my csv. To do that I am reading the existing csv and after doing some check. I am writing csv row to temp file but this temp file add double quote around the string.

I am using fputcsv() for this purpose. Is there any way to escape these double quotes?

$ebayorderfile = 'ebay.csv';
$fh = fopen($ebayorderfile, 'r');
$headings = fgetcsv($fh, 0, ',');
$num = count($headings);

$tempfile = tempnam("/orders","tmp");
$output = fopen($tempfile,'w');
fputcsv($output,$headings);

    while (($row = fgetcsv($fh, 0, ',')) !== FALSE) {   
    $row++;
    $lineString=array();
        for ($c=0; $c < $num; $c++) {
            if(strlen($row[$c])<1){             
                $row[$c] = "\N";
            }                   
            array_push($lineString,$row[$c]);           
        }
        fputcsv($output,$lineString);   
    }

fclose($fh);
fclose($output);
unlink($ebayorderfile);
rename($tempfile,$ebayorderfile);
Ram Sharma
  • 8,676
  • 7
  • 43
  • 56
  • It will only double quote if you put the double quotes in yourself; show a code example of how you are doing this to demonstrate your problem – Mark Baker Jan 14 '15 at 13:25
  • @MarkBaker I have added code in my question, can you please look into this. – Ram Sharma Jan 14 '15 at 13:30
  • Take a look at the optional parameter called "enclosure": http://php.net/manual/en/function.fputcsv.php . By default text is enclosed in quotes ("). – valicu2000 Jan 14 '15 at 13:42

0 Answers0