MNB PHPExcelDeveloper Guide
v2.0.5
CSV module

Reading CSV and TSV Files

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

Updated

LevelIntermediateReading time8 minPackagemnb/mnb-phpexcel
Csv::read()withHeaderRow()projectColumns()rows()

What you will learn

  • Detect the delimiter and source encoding from a sample.
  • Reject malformed or unexpectedly large delimited input.
  • Return associative rows through the shared read session.

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();