note 127865 added to function.str-getcsv
| From: | WadeRossmann at main dot php dot net | Date: | Wed, 26 Oct 2022 21:34:05 +0000 |
| Subject: | note 127865 added to function.str-getcsv | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-219817@lists.php.net to get a copy of this message | ||
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;
}
----
Server IP: 45.112.84.4
Probable Submitter: 45.91.159.161 (proxied: 24.68.114.134)
----
Manual Page -- https://php.net/manual/en/function.str-getcsv.php
Edit -- https://main.php.net/note/edit/127865
Del: integrated -- https://main.php.net/note/delete/127865/integrated
Del: useless -- https://main.php.net/note/delete/127865/useless
Del: bad code -- https://main.php.net/note/delete/127865/bad+code
Del: spam -- https://main.php.net/note/delete/127865/spam
Del: non-english -- https://main.php.net/note/delete/127865/non-english
Del: in docs -- https://main.php.net/note/delete/127865/in+docs
Del: other reasons-- https://main.php.net/note/delete/127865
Reject -- https://main.php.net/note/reject/127865
Search -- https://main.php.net/manage/user-notes.php