Skip to content

Core\Csv::format

Writes rows of text as one CSV document, the format every spreadsheet program can open.

You give Core\Csv::format an array of rows. Each row is an array of strings, and its fields are written in that order. A field that has a comma, a quote or a line break is put in quotes. So is a field that starts or ends with a space, because many programs remove such a space. A quote inside a quoted field is written twice. You never have to quote anything yourself. Every record ends with a newline.

The header option writes column names as the first record. The separator and quote options set the characters used, for a program that expects something other than a comma and ". The lineEnd option is "\n" by default. Set it to "\r\n" for a program that needs CR and LF at the end of each record.

Good to know: the keys of a row are ignored. Only its values are written, in the order they have. Core\Csv::parse reads back exactly the fields you wrote.

Parameters

  • $rowsarray<array<string>>The records to write. Each record is an array of string fields, written in order, and its keys are ignored. A field can be a tainted string, such as a value read from a database. Then the result is a tainted string too.

Options

  • separatorstringoptionalThe one ASCII character written between fields. The default is ,.
  • quotestringoptionalThe one ASCII character that quotes a field. The default is ".
  • headerarray<string>optionalColumn names, written as the first record. The default is null, which writes no header row.
  • lineEndstringoptionalThe characters at the end of each record: "\n" or "\r\n". The default is "\n". Excel and RFC 4180 use "\r\n".

Return value

Returns string. The CSV document. It is an empty string when there are no rows and no header.

Errors

  • RuntimeErrorAn option for the CSV format is empty, longer than one ASCII character, or CR or LF. It is also thrown when separator and quote are the same character, and when lineEnd is not "\n" or "\r\n".

Examples

Write rows as a document

<?nvs
// Shows how to write rows of text as one CSV document. `Core\Csv::format` puts
// a comma between the fields and a newline after every record.
array<array<string>> $rows = [
['fig', '2', '3.50'],
['apple', '10', '0.40'],
];
// This prints `fig,2,3.50` and `apple,10,0.40` on two lines.
echo Core\Csv::format($rows);
Output
fig,2,3.50
apple,10,0.40

Fields that need quotes

<?nvs
// Shows what happens to a field that holds a comma, a quote or a line break.
// `Core\Csv::format` puts quotes around that field and writes a quote inside it
// twice. A program that reads the document gets your text back unchanged.
array<array<string>> $rows = [
['Beans, dried', '500 g'],
["The \"big\" tin", '2 kg'],
["Two lines\nin one field", '1 kg'],
];
string $document = Core\Csv::format($rows);
echo $document;
// The text is unchanged: this prints `Beans, dried`.
var $back = Core\Csv::parse($document);
echo $back[0][0], "\n";
Output
"Beans, dried",500 g
"The ""big"" tin",2 kg
"Two lines
in one field",1 kg
Beans, dried

Export a report for a spreadsheet

<?nvs
// Shows how to export a report that somebody opens in a spreadsheet. The
// `header` option writes the column names as the first record. The `separator`
// option sets the character between the fields, because spreadsheet programs in
// some countries expect a semicolon.
array<string> $columns = ['order', 'customer', 'total'];
array<array<string>> $orders = [
['1042', 'Ada Lovelace', '38.90'],
['1043', 'Grace Hopper', '12.00'],
];
string $report = Core\Csv::format($orders, { header: $columns, separator: ';' });
// The first line is the column names, then one line per order.
echo $report;
Output
order;customer;total
1042;Ada Lovelace;38.90
1043;Grace Hopper;12.00