fputcsv
(PHP 5 >= 5.1.0, PHP 7, PHP 8)
Format line as CSV and write to file pointer
Description
fputcsv() formats a line (passed as a fields array) as CSV and writes it (terminated by a eol) to the specified stream.
Parameters
streamAll
fieldsAn array of
strings.eolThe optional
eolparameter sets a custom End of Line sequence.
Escape-parameter
If an enclosure character is contained in a field, it will be escaped by doubling it, unless it is immediately preceded by an escape.
Return Values
Returns the length of the written string Falseforfailure.
Changelog
| Version | Description |
|---|---|
| 8.1.0 | The optional eol parameter has been added. |
| 7.4.0 | The escape parameter now also accepts an empty string to disable the proprietary escape mechanism. |
Examples
fputcsv() example
<?php
$list = [
['aaa', 'bbb', 'ccc', 'dddd'],
['123', '456', '789'],
['"aaa"', '"bbb"']
];
$fp = fopen('file.csv', 'w');
foreach ($list as $fields) {
fputcsv($fp, $fields, ',', '"', '');
}
fclose($fp);
?>The above example will write the following to file.csv:
aaa,bbb,ccc,dddd
123,456,789
"""aaa""","""bbb"""See Also
fgetcsv()str_getcsv()SplFileObject::fgetcsvSplFileObject::fputcsvSplFileObject::setCsvControlSplFileObject::getCsvControl