SplFileObject::fputcsv
(PHP 5 >= 5.4.0, PHP 7, PHP 8)
Write a field array as a CSV line
Description
Writes the fields array to the file as a CSV line.
Parameters
fieldsAn array of values.
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 stringFalseforfailure.
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
SplFileObject::fputcsv example
<?php
$list = array (
array('aaa', 'bbb', 'ccc', 'dddd'),
array('123', '456', '789'),
array('"aaa"', '"bbb"')
);
$file = new SplFileObject('file.csv', 'w');
foreach ($list as $fields) {
$file->fputcsv($fields);
}
?>The above example will write the following to file.csv:
aaa,bbb,ccc,dddd
123,456,789
"""aaa""","""bbb"""See Also
SplFileObject::fgetcsvSplFileObject::setCsvControlSplFileObject::getCsvControlfputcsv()fgetcsv()str_getcsv()