PHP fputcsv() Function

Definition

The fputcsv() function formats a line as CSV and writes it to an open file.

Syntax

PHP fputcsv() Function has the following syntax.

fputcsv(file,fields,seperator,enclosure)

Parameter

ParameterIs RequiredDescription
fileRequired.Open file to write to
fieldsRequired.Specifies which array to get the data from
separatorOptional.A character that specifies the field separator. Default is comma ( , )
enclosureOptional.A character that specifies the field enclosure character. Default is "

Return

This function returns the length of the written string, or FALSE on failure.

Example

formats a line as CSV and writes it to an open file


<?php/*from   ww  w .j  a v  a 2s  .  c o m*/
$list = array("A,B,C,D","E,F,G,H",);

$file = fopen("contacts.csv","w");

foreach ($list as $line){
  fputcsv($file,split(',',$line));
}

fclose($file); 
?>




















Home »
  PHP Tutorial »
    Function reference »




PHP Array Functions
PHP Calendar Functions
PHP Class Functions
PHP Data Type Functions
PHP Date Functions
PHP File Functions
PHP Image Functions
PHP Math Functions
PHP MySQLi Functions
PHP SimpleXML Functions
PHP String Functions
PHP XML Functions
PHP Zip Functions