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)

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay