CSV Dialects and Encoding
Detect delimiters, inspect source encodings, convert UTF-16/UTF-32 input, and configure locale-specific output.
Updated
CsvDialect::detectDelimiter()EncodingDetector::detectFile()EncodingDetector::convertFileToUtf8()What you will learn
- Preview detected delimiter and encoding before import.
- Configure detection candidates for known data sources.
- Understand when the reader creates a temporary UTF-8 file.
Inspect the source before reading#
use Mnb\PHPExcel\Support\CsvDialect;
use Mnb\PHPExcel\Support\EncodingDetector;
$encoding = EncodingDetector::detectFile('customers.csv');
$delimiter = CsvDialect::detectDelimiter('customers.csv');
print_r($encoding);
printf("Delimiter: %s\n", json_encode($delimiter));Control detection candidates#
$rows = Csv::read('regional-export.csv', [
'delimiter' => 'auto',
'delimiter_candidates' => [';', ',', "\t"],
'delimiter_sample_lines' => 20,
'encoding' => 'auto',
'encoding_candidates' => ['UTF-8', 'Windows-1252', 'ISO-8859-1'],
])->rows();Understand UTF-16 and UTF-32 handling#
PHP’s CSV parser cannot safely parse NUL-separated UTF-16 or UTF-32 bytes directly. When the target is UTF-8, the reader converts those sources into a temporary UTF-8 file, parses it row by row, and removes the temporary file afterward.
Was this guide useful?Use GitHub issues for corrections, missing examples, or unclear behavior.
Open an issue