update page now
PHP 8.1.34 Released!

Voting

: max(three, eight)?
(Example: nine)

The Note You're Voting On

Wade Rossmann
3 years ago
For completeness, here is a userspace str_putcsv() that is fully compatible with fgetcsv() and fputcsv()'s arguments. Namely $escape and $eol, which all others seem to be omitting.

<?php

function str_putcsv(
    array $fields,
    string $separator = ",",
    string $enclosure = "\"",
    string $escape = "\\",
    string $eol = "\n"
) {
    return implode($separator,
        array_map(
            function($a)use($enclosure, $escape) {
                $type = gettype($a);
                switch($type) {
                    case 'integer': return sprintf('%d', $a);
                    case 'double':  return rtrim(sprintf('%0.'.ini_get('precision').'f', $a), '0');
                    case 'boolean': return ( $a ? 'true' : 'false' );
                    case 'NULL':    return '';
                    case 'string':
                        return sprintf('"%s"', str_replace(
                            [$escape, $enclosure],
                            [$escape.$escape, $escape.$enclosure],
                            $a
                        ));
                    default: throw new TypeError("Cannot stringify type: $type");
                }
            },
            $fields
        )
    ) . $eol;
}

<< Back to user notes page

To Top