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()


#-------------------------------------




-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to