Skip to main content
Version: Fegg 5.x

Csv::toString

Return rows as CSV text

Description​

public static string Csv::toString( iterable $rows [, array $options = array() ] )

Returns the CSV text of $rows, with a BOM first for 'utf-8-bom' (see Writing).

Arguments​

NameData typeDefaultRemarks
$rowsiterableRows: arrays (lists or associative; only the values are written), or a generator
$optionsarrayarray()See below

Options​

KeyDefaultRemarks
headerarray()Titles written as the first row
encoding'utf-8-bom''utf-8-bom', 'utf-8' or 'sjis'
escape_formulastruefalse writes text starting with = + - @ as it is (see Formulas)

Example​

Controller
use Fegg\Tool\Csv;

$csv = Csv::toString(array(
array('Hello, world', 3, true, null),
array('=SUM(A1:A2)', -5, false, new \DateTime('2026-11-15 10:00')),
), array('header' => array('Text', 'Number', 'Flag', 'Date'), 'encoding' => 'utf-8'));
$csv
Text,Number,Flag,Date
"Hello, world",3,1,
'=SUM(A1:A2),-5,0,2026-11-15 10:00:00

Use it to attach a CSV to a mail or save it to a file; download() sends it to the browser.