Format a line as CSV and writes it to an open file:rephrase
<?php $list = array ( array(“Peter”, “Griffin” ,“Oslo”, “Norway”), array(“Glenn”, “Quagmire”, “Oslo”, “Norway”) ); $file = fopen(“contacts.csv”,“w”); foreach ($list as $line) { fputcsv($file, $line); } fclose($file); ?> |
A line is formatted as CSV and written to an open file using the fputcsv() method.
See the fgetcsv() function as well.
fputcsv(file, fields, separator, enclosure, escape) |
Parameter |
Description |
file |
Essential. The open file to write to is specified. |
fields |
Essential. indicates the array from which to retrieve the data. |
separator |
Not required. The field separator is indicated by this character. By default, rephrase is comma (, ) |
enclosure |
Optional. A character that specifies the field enclosure character. Default is “ |
escape |
Optional. Specifies the escape character. Default is “\\”. Can also be an empty string (“”) which disables the escape mechanism |
Return Value: |
The written string’s length in the event of success and FALSE in the event of failure |
PHP Version: |
5.1+ |
PHP Changelog: |
PHP 7.4: To turn off the escape mechanism, you may now pass an empty string as the escape argument. |