note 14788 added to function.fgetcsv.php
| From: | blair at php2 dot chek dot com | Date: | Tue, 14 Aug 2001 23:19:38 +0000 |
| Subject: | note 14788 added to function.fgetcsv.php | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-16724@lists.php.net to get a copy of this message | ||
Here is a function that takes an array and adds a CSV line to the passed file pointer.
#-------------------------------------
function fputcsv ($fp, $array, $deliminator=",") {
$line = "";
foreach($array as $val) {
# remove any windows new lines,
# as they interfere with the parsing at the other end
$val = str_replace("\r\n", "\n", $val);
# if a deliminator char, a double quote char or a newline
# are in the field, add quotes
if(ereg("[$deliminator\"\n\r]", $val)) {
$val = '"'.str_replace('"', '""',
$val).'"';
}#end if
$line .= $val.$deliminator;
}#end foreach
# strip the last deliminator
$line = substr($line, 0, (strlen($deliminator) * -1));
# add the newline
$line .= "\n";
# we don't care if the file pointer is invalid,
# let fputs take care of it
return fputs($fp, $line);
}#end fputcsv()
#-------------------------------------
http://www.php.net/manual/en/function.fgetcsv.php