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
Parameter | Is Required | Description |
---|---|---|
file | Required. | Open file to write to |
fields | Required. | Specifies which array to get the data from |
separator | Optional. | A character that specifies the field separator. Default is comma ( , ) |
enclosure | Optional. | 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);
?>