The fputcsv() function formats a line as CSV and writes it to an open file.
PHP fputcsv() Function has the following syntax.
fputcsv(file,fields,seperator,enclosure)
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 " |
This function returns the length of the written string, or FALSE on failure.
formats a line as CSV and writes it to an open file
<?php/* w w w.jav a 2 s. c om*/
$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);
?>
The following code shows how to format a line as CSV and writes it to an open file.
//w w w . j a v a2s .c om
<?php
$list = array("XML,HTML,CSS,Java","A,B,C,D",);
$file = fopen("contacts.csv","w");
foreach ($list as $line){
fputcsv($file,explode(',',$line));
}
fclose($file);
?>