Reading CSV and TSV Files
Detect delimiters and encodings, enforce structural limits, project columns, and normalize rows during streaming.
Updated
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#
$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#
$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#
$rows = Csv::read('events.tsv', [
'dialect' => 'excel_tab',
'start_row' => 2,
'source_limit_rows' => 5000,
'columns' => [1, 3, 7],
])->withHeaderRow(1)->rows();Was this guide useful?Use GitHub issues for corrections, missing examples, or unclear behavior.
Open an issue