fputcsv
Format the line to CSV and write to the file pointer
fputcsv()
function formats the line to CSV and writes to an open file.
This function returns the length of the written string. If an error occurs, false is returned. .
<?php $list = array ( "George, John, Thomas, USA" , "James,Adrew,Martin,USA" , ) ; $file = fopen ( "contacts.csv" , "w" ) ; foreach ( $list as $line ) { fputcsv ( $file , split ( ',' , $line ) ) ; } fclose ( $file ) ; ?>
After the above code is executed, the CSV file will look like this:
George, John, Thomas, USA James Adrew Martin USA
fputcsv ( file , fields , seperator , enclosure )
parameter | describe |
---|---|
file | Required. Specifies the open file to be written. |
fields | Required. Specifies an array from which data is to be obtained. |
seperator | Optional. Characters that specify field separators. The default is comma (,). |
enclosure | Optional. Characters that specify the field surrounding the character. The default is double quotes". |
fputcsv()
formats a line (passed with fields array) into CSV format and writes to the file specified by the file .