fputcsv and newline codes

前端 未结 8 2228
别那么骄傲
别那么骄傲 2020-11-28 10:12

I\'m using fputcsv in PHP to output a comma-delimited file of a database query. When opening the file in gedit in Ubuntu, it looks correct - each record has a line break (no

相关标签:
8条回答
  • 2020-11-28 11:17

    I did eventually get an answer over at experts-exchange; here's what worked:

    function my_fputcsv($handle, $fieldsarray, $delimiter = "~", $enclosure ='"'){
       $glue = $enclosure . $delimiter . $enclosure;
       return fwrite($handle, $enclosure . implode($glue,$fieldsarray) . $enclosure.PHP_EOL);
    }
    

    to be used in place of standard fputcsv.

    0 讨论(0)
  • 2020-11-28 11:18

    This is an improved version of @John Douthat's great answer, preserving the possibility of using custom delimiters and enclosures and returning fputcsv's original output:

    function fputcsv_eol($handle, $array, $delimiter = ',', $enclosure = '"', $eol = "\n") {
        $return = fputcsv($handle, $array, $delimiter, $enclosure);
        if($return !== FALSE && "\n" != $eol && 0 === fseek($handle, -1, SEEK_CUR)) {
            fwrite($handle, $eol);
        }
        return $return;
    }
    
    0 讨论(0)
提交回复
热议问题