note 108463 added to function.fputcsv
| From: | HirotoKagotani | Date: | Sat, 28 Apr 2012 03:14:02 +0000 |
| Subject: | note 108463 added to function.fputcsv | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-187386@lists.php.net to get a copy of this message | ||
In order to solve the problem of a backslash followed by a double quote
(https://bugs.php.net/bug.php?id=43225), I wrote a new function.
<?php
function my_fputcsv($handle, $fields, $delimiter = ',', $enclosure = '"',
$escape = '\\')
{
$first = 1;
foreach ($fields as $field) {
if ($first == 0) fwrite($handle, ",");
$f = str_replace($enclosure, $enclosure.$enclosure, $field);
if ($enclosure != $escape) {
$f = str_replace($escape.$enclosure, $escape, $f);
}
if (strpbrk($f, " \t\n\r".$delimiter.$enclosure.$escape) || strchr($f,
"\000")) {
fwrite($handle, $enclosure.$f.$enclosure);
} else {
fwrite($handle, $f);
}
$first = 0;
}
fwrite($handle, "\n");
}
?>
It is compatible to fputcsv() except for the additional 5th argument $escape, which has the same
meaning as that of fgetcsv(). If you set it to '"' (double quote), every double
quote is escaped by itself.
----
Server IP: 69.147.83.197
Probable Submitter: 123.223.135.68
----
Manual Page -- http://www.php.net/manual/en/function.fputcsv.php
Edit -- https://master.php.net/note/edit/108463
Del: integrated -- https://master.php.net/note/delete/108463/integrated
Del: useless -- https://master.php.net/note/delete/108463/useless
Del: bad code -- https://master.php.net/note/delete/108463/bad+code
Del: spam -- https://master.php.net/note/delete/108463/spam
Del: non-english -- https://master.php.net/note/delete/108463/non-english
Del: in docs -- https://master.php.net/note/delete/108463/in+docs
Del: other reasons-- https://master.php.net/note/delete/108463
Reject -- https://master.php.net/note/reject/108463
Search -- https://master.php.net/manage/user-notes.php