Skip to content
MNB PHPExcelDeveloper Guide
AI

CSV library

Reading CSV and TSV Files

Detect delimiters and encodings, enforce structural limits, project columns, and normalize rows during streaming.

Level
Intermediate
Reading time
8 min
Package
mnb/mnb-phpexcel-csv
Updated
Csv::read()withHeaderRow()projectColumns()rows()

Read with automatic dialect and encoding detection#

PHP
$rows = Csv::read('inventory.csv', [
    'delimiter' => 'auto',
    'encoding' => 'auto',
    'target_encoding' => 'UTF-8',
    'trim_values' => true,
    'empty_strings_to_null' => true,
    'ignore_blank_lines' => true,
])->withHeaderRow(1)->streaming()->rows();

Enforce upload boundaries#

PHP
$rows = Csv::read('upload.csv', [
    'max_file_bytes' => 20 * 1024 * 1024,
    'max_source_rows' => 250000,
    'max_columns' => 80,
    'strict_column_count' => true,
    'expected_columns' => 12,
])->rows();

strict_column_count can infer the expected count from the first delivered row or use an explicit expected_columns value.

Read a slice and project columns#

PHP
$rows = Csv::read('events.tsv', [
    'dialect' => 'excel_tab',
    'start_row' => 2,
    'source_limit_rows' => 5000,
    'columns' => [1, 3, 7],
])->withHeaderRow(1)->rows();