The League of Extraordinary Packages

Our Packages:

Presented by The League of Extraordinary Packages

Getting Started

Connections

Inserting Records

Selecting Records

Converting Records

Upgrading Guide

This is the documentation for the upcoming version 9.0. This is a work in progress

BOM sequences

Detecting the BOM sequence

<?php

const BOM::UTF8 = "\xEF\xBB\xBF";
const BOM::UTF16_BE = "\xFE\xFF";
const BOM::UTF16_LE = "\xFF\xFE";
const BOM::UTF32_BE = "\x00\x00\xFE\xFF";
const BOM::UTF32_LE = "\xFF\xFE\x00\x00";

To improve interoperability with programs interacting with CSV, the package now provide an interface BOM to help you detect set the appropriate BOM sequence.

Constants

To ease BOM sequence manipulation, the BOM class provides the following constants :

bom_match

<?php

function bom_match(string $str): string

The League\Csv\bom_match function expects a string and returns the BOM sequence found at its start or an empty string otherwise.

<?php

use League\Csv\BOM;
use function League\Csv\bom_match;

bom_match('hello world!'); //returns ''
bom_match(BOM::UTF8.'hello world!'); //returns '\xEF\xBB\xBF'
bom_match('hello world!'.BOM::UTF16_BE); //returns ''

Managing CSV documents BOM sequence

Detecting the BOM sequence

<?php

public AbstractCsv::getInputBOM(void): string

The CSV document current BOM character is detected using the getInputBOM method. This method returns the currently used BOM character or an empty string if none is found or recognized. The detection is done using the bom_match function.

<?php

use League\Csv\Writer;

$csv = Writer::createFromPath('/path/to/file.csv');
$bom = $csv->getInputBOM();

Setting the outputted BOM sequence

<?php

public AbstractCsv::setOutputBOM(string $sequence): self
public AbstractCsv::getOutputBOM(void): string
<?php

use League\Csv\BOM;
use League\Csv\Reader;

$csv = Reader::createFromPath('/path/to/file.csv');
$csv->setOutputBOM(BOM::UTF8);
$bom = $csv->getOutputBOM(); //returns "\xEF\xBB\xBF"

The default output BOM character is set to an empty string.

The output BOM sequence is never saved to the CSV document.