league/csv
CSV data manipulation made easy in PHP
Repository Health
Technical Analysis
league/csv is a PHP library for reading, writing, and manipulating CSV data with a fluent, stream-based API. It provides a Reader and Writer built around PHP’s native SplFileObject/stream filters, a query layer for filtering, sorting, and selecting records without loading the whole file into memory, and converters for exporting CSV data to JSON, XML, and HTML.
Maintained by The League of Extraordinary Packages, it integrates with PDO, SQLite3, MySQLi, and PgSQL result sets, supports BOM detection and charset conversion, and guards against CSV formula injection when exporting to spreadsheet-consumed files. It targets modern PHP (8.2+) and is one of the most widely used CSV libraries in the PHP ecosystem.
What You Get
- A
Readerclass for iterating CSV records lazily via generators, without loading the entire file into memory - A
Writerclass with configurable delimiters, enclosures, escape characters, and newline sequences - A fluent query layer (
Query\Statement) for filtering, sorting, and selecting a subset of records - Converters for exporting CSV data to JSON, XML, and HTML tables
- BOM detection/stripping, charset conversion, and
EscapeFormulaprotection against CSV injection in spreadsheet apps
Common Use Cases
- Importing large CSV files into a database or in-memory structures without exhausting memory
- Exporting query results (from PDO, MySQLi, PgSQL, or SQLite3) directly to a downloadable CSV file
- Building CSV-based ETL or data-transformation pipelines with filtering and column selection
- Safely generating CSV exports for spreadsheet consumption, protected against formula-injection attacks
Under The Hood
Architecture - The library centers on an AbstractCsv base class shared by Reader and Writer, both wrapping a PHP stream resource (typically an SplFileObject or a raw stream) rather than reading the whole document into memory at once; records are exposed as PHP generators so a Reader can iterate multi-gigabyte files with constant memory. A separate Query namespace implements a statement/predicate system (filter, sort, offset/limit) that operates on the same lazy iterator, letting query criteria compose without materializing intermediate arrays. Format-conversion classes (JsonConverter, XMLConverter, HTMLConverter) consume the same Reader iterator interface, and CharsetConverter/Bom handle encoding detection and normalization at the stream-filter level via PHP’s stream_filter_register. Tech Stack - Pure PHP 8.2+, using PSR-4 autoloading (League\Csv namespace) and native PHP extensions (ext-filter, optionally ext-dom, ext-iconv, ext-mbstring, ext-pdo) rather than third-party dependencies; static analysis is enforced with PHPStan (including strict and deprecation-rule extensions) and code style with PHP-CS-Fixer. Code Quality - Test files are colocated with source files (e.g. ReaderTest.php next to Reader.php, 123 PHP source files across src/) using PHPUnit across three supported major versions (10/11/12), plus dedicated *Bench.php benchmark files run via phpbench for reader/writer performance regressions — an unusually rigorous quality bar combining unit tests, static analysis, and continuous benchmarking. API Design - The library exposes small, named entry points (Reader::createFromPath, Writer::createFromPath) and a fluent, chainable query builder (Statement::create()->where(...)->orderBy(...)), keeping common read/write use cases to one or two lines while still surfacing lower-level stream-filter hooks (EscapeFormula, CharsetConverter) for advanced encoding and security needs; the dedicated documentation site (csv.thephpleague.com) mirrors this progressive-disclosure structure.
Used by 7 apps in this directory
Appwrite
Developer Tools · Databases · Authentication
Open-source backend platform with Auth, Databases, Storage, Functions, Messaging, Realtime, and Sites — deploy via Docker or use Appwrite Cloud.
Invoice Ninja
Invoicing Finance · Project Management
Self-hostable invoicing, time-tracking, and multi-gateway payment platform for freelancers and small businesses, with built-in e-invoicing compliance for EU and global standards.
Kimai
Invoicing Finance · Project Management
Professional open-source time tracking with invoicing, multi-user support, SAML/LDAP auth, and a full REST API—self-host it or use the cloud.
Leantime
Productivity · Project Management · Collaboration
Project management built for ADHD, autism, and dyslexia — simple as Trello, powerful as Jira
Shlink
Developer Tools · Marketing
Self-hosted PHP URL shortener with a full REST API, dynamic redirect rules, and real-time visit analytics under your own domain.
solidtime
Productivity · Invoicing Finance
Modern open-source time tracker for freelancers and agencies with invoicing, multi-org support, and Toggl/Clockify migration built in.
Yaffa
Invoicing Finance
Self-hosted personal finance app for long-term financial planning with AI-powered transaction parsing and investment tracking.