Re: fputcsv() (was: best way to read a file)
| From: | bvr | Date: | Fri, 29 Mar 2002 01:32:28 +0000 |
| Subject: | Re: fputcsv() (was: best way to read a file) | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-90635@lists.php.net to get a copy of this message | ||
Didn't test.
http://www.php.net/manual/en/function.fgetcsv.php
*blair at squiz*
14-Aug-2001 07:19
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()
#-------------------------------------