Multibyte delimiter
The SwapDelimiter is a PHP stream filter which enables converting the multibytes delimiter into a
suitable delimiter character to allow processing your CSV document.
Usage with CSV objects
Out of the box, the package is not able to handle multibytes delimited CSV. You should first try to see if by changing your PHP locale settings the CSV gets correctly parsed.
use League\Csv\SwapDelimiter;
use League\Csv\Reader;
$document = <<<CSV
csv;content;in;japanese;locale
CSV;
setlocale(LC_ALL, 'ja_JP.SJIS');
$reader = Reader::fromString($document);
$reader->setHeaderOffset(0);
$reader->first();
If that does not work you can then try using the SwapDelimiter stream filter.
public static SwapDelimiter::addTo(AbstractCsv $csv, string $sourceDelimiter): void
The SwapDelimiter::addTo method will:
- register the stream filter if it is not already the case.
- add a stream filter using the specified CSV delimiter.
- for the
Writerobject it will convert the CSV single-byte delimiter into the$sourceDelimiter - for the
Readerobject it will convert the$sourceDelimiterdelimiter into a CSV single-byte delimiter
- for the
use League\Csv\SwapDelimiter;
use League\Csv\Writer;
$writer = Writer::fromString();
$writer->setDelimiter("\x02");
SwapDelimiter::addTo($writer, '💩');
$writer->insertOne(['toto', 'tata', 'foobar']);
$writer->toString();
//returns toto💩tata💩foobar\n
Once the SwapDelimiter::addTo is called you should not change your CSV delimiter setting. Or put in
other words. You should first set the CSV single-byte delimiter before calling the SwapDelimiter method.
Conversely, you can use the same technique with a Reader object.
use League\Csv\SwapDelimiter;
use League\Csv\Reader;
$document = <<<CSV
observedOn💩temperature💩place
2023-10-01💩18💩Yamoussokro
2023-10-02💩21💩Yamoussokro
2023-10-03💩15💩Yamoussokro
CSV;
$reader = Reader::fromString($document);
$reader->setHeaderOffset(0);
$reader->setDelimiter("\x02");
SwapDelimiter::addTo($reader, '💩');
$reader->first();
//returns ['observedOn' => '2023-10-01', 'temperature' => '18', 'place' => 'Yamoussokro']