note 66909 deleted from function.fgetcsv by danbrown
| From: | danbrown@php.net | Date: | Sun, 17 May 2009 18:13:54 +0000 |
| Subject: | note 66909 deleted from function.fgetcsv by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-155082@lists.php.net to get a copy of this message | ||
Note Submitter: Ariel asphp at dsgml dot com
----
Should you need it, here is a nice and simple function for escaping csv fields properly.
This version is conditional - it only adds quotes if needed:
<?
function csv_escape($str) {
$str = str_replace(array('"', ',', "\n", "\r"),
array('""', ',', "\n", "\r"), $str, &$count);
if($count) {
return '"' . $str . '"';
} else {
return $str;
}
}
?>
This version is even simpler, but adds quotes even if not needed.
<?
function csv_escape($str) {
return = '"' . str_replace('"','""', $str) .
'"';
}
?>