note 22582 modified in function.fgetcsv by danbrown
| From: | danbrown@php.net | Date: | Sun, 17 May 2009 18:23:46 +0000 |
| Subject: | note 22582 modified in function.fgetcsv by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-155110@lists.php.net to get a copy of this message | ||
Quoting csv is pretty simple, and there are two steps..I note in previous comments only the second
step has been explained.
First fields with double quotes need to double up the quotes, ie _He said "Timmy"..._
should become _He said ""Timmy""..._
Secondly as mentioned above, fields with commas need double quotes.
Here is a simple function to achieve this, that I pass to array walk, if you want to use it
somewhere else, prob get rid of reference and return the value.
<?php
function check_csv_field_ref(&$item) {
$item = str_replace('"', '""', $item);
if (strpos($item, ",") !== FALSE) {
$item = '"' . $item . '"';
}
}
?>
--was--
Quoting csv is pretty simple, and there are two steps..I note in previous comments only the second
step has been explained.
First fields with double quotes need to double up the quotes, ie _He said "Timmy"..._
should become _He said ""Timmy""..._
Secondly as mentioned above, fields with commas need double quotes.
Here is a simple function to achieve this, that I pass to array walk, if you want to use it
somewhere else, prob get rid of reference and return the value.
function check_csv_field_ref(&$item) {
$item = str_replace('"', '""', $item);
if (strpos($item, ",") !== FALSE) {
$item = '"' . $item . '"';
}
}
http://php.net/manual/en/function.fgetcsv.php