DEV Community

Dev-Sandbox
Dev-Sandbox

Posted on

FPUTCSV Custom Function

Requirements
Values Surrounded by Double Quotes Always.
Escape Character is "
CR+LF is the newline

function fputcsv_custom($stream, $fields, $delimiter = ",", $enclosure = '"', $escape_char = '\"', $eol = "\r\n") {
    $field_arr = [];

    foreach($fields as $field) {
        $field_arr[] = $enclosure . stripslashes(str_replace($enclosure, $escape_char . $enclosure, $field)) . $enclosure;
    }

    fwrite($stream, implode($delimiter, $field_arr) . $eol);
}
Enter fullscreen mode Exit fullscreen mode

Ref: https://stackoverflow.com/questions/16942531/alternative-to-fputcsv/66682050#66682050

Top comments (0)