CSV data manipulation made easy in PHP

Overview

CSV

Latest Version Software License Build Total Downloads

Csv is a simple library to ease CSV parsing, writing and filtering in PHP. The goal of the library is to be powerful while remaining lightweight, by utilizing PHP native classes whenever possible.

Highlights

  • Simple API
  • Read and Write to CSV documents in a memory efficient and scalable way
  • Support PHP stream filtering capabilities
  • Transform CSV documents into popular format (JSON, XML or HTML)
  • Fully documented
  • Fully unit tested
  • Framework-agnostic

Documentation

Full documentation can be found at csv.thephpleague.com.

System Requirements

You need PHP >= 7.3 and the mbstring extension to use Csv but the latest stable version of PHP is recommended.

Install

Install Csv using Composer.

$ composer require league/csv

Configuration

Warning: If your CSV document was created or is read on a Macintosh computer, add the following lines before using the library to help PHP detect line ending.

if (!ini_get("auto_detect_line_endings")) {
    ini_set("auto_detect_line_endings", '1');
}

Testing

League\Csv has a :

  • a PHPUnit test suite
  • a coding style compliance test suite using PHP CS Fixer.
  • a code analysis compliance test suite using PHPStan.

To run the tests, run the following command from the project folder.

$ composer test

Contributing

Contributions are welcome and will be fully credited. Please see CONTRIBUTING and CONDUCT for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Changelog

Please see CHANGELOG for more information on what has changed recently.

Credits

License

The MIT License (MIT). Please see LICENSE for more information.

You might also like...
PHP stream wrapper for Flysystem v2

Flystream enables you to use core PHP filesystem functions to interact with Flysystem filesystems by registering them as custom protocols.

File manager module for the Lumen PHP framework.

Lumen File Manager File manager module for the Lumen PHP framework. Please note that this module is still under active development. NOTE: Branch 5.1 i

This small PHP package assists in the loading and parsing of VTT files.

VTT Transcriptions This small PHP package assists in the loading and parsing of VTT files. Usage use Laracasts\Transcriptions\Transcription; $transcr

一个轻量级的 PHP 文件编辑器

AdminX 一个轻量级的 PHP 文件编辑器 安装 只需要前往 Release 页面下载最新版本的 adminx.php 放在你的主机文件里即可 若需要实时更新的 Develop Version 可以前往源代码找到 adminx.php 即可 配置 所有可配置的选项均在 adminx.php 的前

A full PHP implementation of Minecraft's Named Binary Tag (NBT) format.

php-nbt A full PHP implementation of Minecraft's Named Binary Tag (NBT) format. In contrast to other implementations, this library provides full suppo

A php sharex uploader with discord embed function/twitter card support

Sharex Uploader Simple Sharex Uploader with Discord embed function Download replace your data and upload to your sevrer

Gotipath Storage is a file storage library for PHP. It provides one interface to interact with FTP/SFTP.

Gotipath Storage is a file storage library for PHP. It provides one interface to interact with FTP/SFTP. When you use this package, you're protected from vendor lock-in, That mean you can connect to any FTP/SFTP storage. Also it's comes with base URL option to connect Gotipath CDN.

PHPProject is a library written in pure PHP that provides a set of classes to write to different project management file formats

PHPProject PHPProject is a library written in pure PHP that provides a set of classes to write to different project management file formats, i.e. Micr

Releases(9.8.0)
  • 9.8.0(Jan 4, 2022)

    Added

    • Added PHP7.4 typed properties where applicable
    • TabularDataReader::fetchColumnByName to replace TabularDataReader::fetchColumn
    • TabularDataReader::fetchColumnByOffset to replace TabularDataReader::fetchColumn

    Deprecated

    • TabularDataReader::fetchColumn use TabularDataReader::fetchColumnByOffset or TabularDataReader::fetchColumnByName instead

    Fixed

    • AbstractCsv constructor is marked final via docblock.
      The method should never be extended or changed in child classes to avoid unexpected behaviour

    Removed

    • PHP7.3 support
    • Remove internal EmptyEscapeParser Polyfill used in Reader class
    • Remove PHP7.4 polyfill features in Writer class
    Source code(tar.gz)
    Source code(zip)
  • 9.7.4(Nov 30, 2021)

  • 9.7.3(Nov 21, 2021)

    Added

    • None

    Deprecated

    • None

    Fixed

    • Update PHPStan requirement for development
    • Improve Documentation generation thanks to pdelre
    • PHP8.1 compliance: using ReturnTypeWillChange to avoid emitting a unnecessary deprecation notice. thanks to cedric-anne

    Removed

    • None
    Source code(tar.gz)
    Source code(zip)
  • 9.7.2(Oct 5, 2021)

    Added

    • None

    Deprecated

    • None

    Fixed

    • Update dependencies requirement for development
    • PHP8.1 compliance: replace FILTER_SANITIZE_STRING by FILTER_UNSAFE_RAW
    • PHP8.1 compliance: remove duplicated public properties declaration
    • PHP8.1 compliance: add support for fputcsv $eol argument

    Removed

    • None
    Source code(tar.gz)
    Source code(zip)
  • 9.7.1(Apr 17, 2021)

  • 9.7.0(Mar 26, 2021)

    Added

    • League\Csv\SyntaxError::duplicateColumnNames to expose column name duplicates during header usage
    • League\Csv\UnableToProcessCsv as the new Exception Marker Interface
    • League\Csv\UnavailableStream as the new Exception
    • League\Csv\Info::getDelimiterStats to replace the namespace function delimiter_detect
    • League\Csv\Info::fetchBOMSequence to replace the namespace function bom_match
    • League\Csv\AbstractCsv::toString to replace League\Csv\AbstractCsv::getContent and League\Csv\AbstractCsv::__toString
    • League\Csv\XMLConverter::create to replace League\Csv\XMLConverter::__construct
    • League\Csv\HTMLConverter::create to replace League\Csv\HTMLConverter::__construct
    • League\Csv\AbstractCsv::supportsStreamFilterOnRead and League\Csv\AbstractCsv::supportsStreamFilterOnWrite to replace League\Csv\AbstractCsv::supportsStreamFilter and League\Csv\AbstractCsv::getStreamFilterMode

    Deprecated

    • League\Csv\delimiter_detect use League\Csv\Info::getDelimiterStats
    • League\Csv\bom_match use League\Csv\Info::fetchBOMSequence
    • League\Csv\AbstractCsv::getContent use League\Csv\AbstractCsv::toString
    • League\Csv\AbstractCsv::getStreamFilterMode use League\Csv\AbstractCsv::supportsStreamFilterOnRead or League\Csv\AbstractCsv::supportsStreamFilterOnWrite
    • League\Csv\AbstractCsv::supportsStreamFilter use League\Csv\AbstractCsv::supportsStreamFilterOnRead or League\Csv\AbstractCsv::supportsStreamFilterOnWrite
    • Calling exceptions constructor, use named constructors instead.
    • League\Csv\XMLConverter::__construct use League\Csv\XMLConverter::create
    • League\Csv\HTMLConverter::__construct use League\Csv\HTMLConverter::create

    Fixed

    • Move tests into the src directory
    • Fixed encoder method resolver implementation
    • all classes marked as @internal are now final
    • League\Csv\AbstractCsv::STREAM_FILTER_MODE constant replaces League\Csv\AbstractCsv::$stream_filter_mode

    Removed

    • Support for PHP7.2
    • League\Csv\AbstractCsv::$stream_filter_mode
    Source code(tar.gz)
    Source code(zip)
  • 9.6.2(Dec 10, 2020)

  • 9.6.1(Sep 5, 2020)

  • 9.6.0(Mar 17, 2020)

    Added

    • More return types and type parameters as supported in PHP7.2+
    • League\Csv\Statement::create named constructor to ease constraint builder instantiation
    • League\Csv\Statement can now also process League\Csv\ResultSet instances.
    • League\Csv\TabularDataReader interface to represent how to read tabular data
    • League\Csv\ResultSet::getRecords has an optional $header second argument to make the method works like League\Csv\Reader::getRecords
    • League\Csv\ResultSet::createFromTabularDataReader create a new instance from League\Csv\TabularDataReader implementing class.

    Deprecated

    • Nothing

    Fixed

    • League\Csv\Reader no longer uses __call to implement fetchOne, fetchPairs and fetchColumn methods.

    Removed

    • Internal polyfill for is_iterable
    • Internal polyfill for is_nullable_int
    • Support for PHP7.0 and PHP7.1
    Source code(tar.gz)
    Source code(zip)
  • 9.5.0(Dec 15, 2019)

    Added

    • Improve package exception throwing by adding new exceptions classes that extends the League\Csv\Exception exception marker class #360, #361 feature proposed and developed by Darren Miller
      • League\Csv\UnavailableFeature
      • League\Csv\InvalidArgument
      • League\Csv\SyntaxError

    Deprecated

    • Nothing

    Fixed

    • bug fix bom_match function see issue #363 resolved based on PR from Jerry Martinez
    • bug fix delemiter_detect function see issue #366

    Removed

    • Nothing
    Source code(tar.gz)
    Source code(zip)
  • 9.4.1(Oct 17, 2019)

  • 9.4.0(Oct 2, 2019)

    Added

    • Adding support for controlling empty record presence in Reader::getRecords return value.

      • Reader::includeEmptyRecords
      • Reader::skipEmptyRecords
      • Reader::isEmptyRecordsIncluded
    • Adding support for controlling Input BOM usage in the library:

      • AbstractCsv::skipInputBOM
      • AbstractCsv::includeInputBOM
      • AbstractCsv::isInputBOMIncluded

    Deprecated

    • Nothing

    Fixed

    • EmptyEscapeParser::parse no longer auto skip empty records

    Removed

    • Nothing
    Source code(tar.gz)
    Source code(zip)
  • 9.3.0(Jul 30, 2019)

    Added

    • XMLConverter::import see #348 thanks @kusabi
    • Support for thead, tfoot and tbody in HTMLConverter::convert via the addition of protected methods HTMLConverter::addHTMLAttributes and HTMLConverter::appendHeaderSection #348 thanks @kusabi

    Deprecated

    • Nothing

    Fixed

    • Internal improvement in Reader dockblock thanks @ghobaty.
    • Improve strict comparison when using preg_match.
    • Improve CSV control in Stream.

    Removed

    • Nothing
    Source code(tar.gz)
    Source code(zip)
  • 9.2.1(Jun 8, 2019)

    Added

    • Nothing

    Fixed

    • AbstractCSV::chunk see #325 remove CSV flags from the Stream class to avoid infinite loop.
    • Internal improve HTMLConverter.

    Deprecated

    • Nothing

    Removed

    • Nothing
    Source code(tar.gz)
    Source code(zip)
  • 9.2.0(Mar 8, 2019)

    Added

    • Supports for PHP7.4 empty string for the escape character.
    • Supports for empty string for the escape character with a polyfill for PHP7.4- versions.
    • AbstractCSV::getPathname see #321 thanks @tomkyle

    Deprecated

    • League\Csv\RFC4180Field use AbstractCSV::setEscape method with an empty string instead.

    Fixed

    • AbstractCSV::__construct correctly initializes properties

    • AbstractCSV::createFromString named constructor default argument is now the empty string

    • AbstractCSV::setEscape now accepts the empty string like fputcsv and fgetcsv

    • Writer::insertOne fixes throwing exception when record can not be inserted

    • XMLConverter convert to string the record value to avoid PHP warning on null value

    • Internal Stream::createFromString named constructor default argument is now the empty string

    • Internal Stream::fwrite improved

    • Internal Stream::__destruct no longer emit warning on invalid stream filter removal.

    • Internal Stream::seek returns 0 if the seeked position 0 is valid see #321 thanks @HaozhouChen

    • Reader:getHeader when the record is an empty line.

    Removed

    • Nothing
    Source code(tar.gz)
    Source code(zip)
  • 9.1.4(May 1, 2018)

    Added

    • Nothing

    Deprecated

    • Nothing

    Fixed

    • Writer::setFlushThreshold should accept 1 as an argument #289

    • CharsetConverter::convert should not try to convert numeric value #287

    Removed

    • Nothing
    Source code(tar.gz)
    Source code(zip)
  • 9.1.3(Mar 12, 2018)

    Added

    • Nothing

    Deprecated

    • Nothing

    Fixed

    • Writer::insertOne allow empty array to be added to the CSV (allow inserting empty row)
    • Removed all return type from named constructor see #285
    • Added PHPStan for static code analysis

    Removed

    • Nothing
    Source code(tar.gz)
    Source code(zip)
  • 8.2.3(Feb 6, 2018)

  • 9.1.2(Feb 5, 2018)

  • 9.1.1(Nov 28, 2017)

  • 9.1.0(Oct 20, 2017)

    Added

    • Support for non seekable stream. When seekable feature are required an exceptions will be thrown.
    • League\Csv\EncloseField to force enclosure insertion on every field. #269
    • League\Csv\EscapeFormula a League CSV formatter to prevent CSV Formula Injection in Spreadsheet programs.
    • League\Csv\RFC4180Field::addTo accept an option $replace_whitespace argument to improve RFC4180 compliance.
    • League\Csv\Abstract::getContent to replace League\Csv\Abstract::__toString. The __toString method may trigger a Fatal Error with non seekable stream, instead you are recommended to used the new getContent method which will trigger an exception instead.

    Deprecated

    • League\Csv\Abstract::__toString use League\Csv\Abstract::getContent instead. the __toString triggers a Fatal Error when used on a non-seekable CSV document. use the getContent method instead which will trigger an exception instead.

    Fixed

    • Bug fixes headers from AbstractCsv::output according to RFC6266 #250
    • Make sure the internal source still exists before closing it #251
    • Make sure the Reader::createFromPath default open mode is r see #258 and #266

    Removed

    • Nothing
    Source code(tar.gz)
    Source code(zip)
  • 9.0.1(Aug 22, 2017)

  • 9.0.0(Aug 18, 2017)

    Added

    • Improved CSV Records selection
      • League\Csv\Reader::getRecords to access all CSV records
      • League\Csv\Statement provides a constraint builder to select CSV records.
      • League\Csv\ResultSet represents the result set of the selected CSV records.
      • League\Csv\delimiter_detect function to detect CSV delimiter character
    • Improved CSV document header selection.
      • League\Csv\Reader::getHeader
      • League\Csv\Reader::getHeaderOffset
      • League\Csv\Reader::setHeaderOffset
    • Improved CSV Records conversion
      • League\Csv\CharsetConverter converts CSV records charset.
      • League\Csv\XMLConverter converts CSV records into DOMDocument
      • League\Csv\HTMLConverter converts CSV records into HTML table.
    • Improved Exception handling
      • League\Csv\Exception the default exception
      • League\Csv\CannotInsertRecord
    • Improved CSV document output
      • League\Csv\AbstractCsv::chunk method to output the CSV document in chunk
      • League\Csv\bom_match function to detect BOM sequence in a given string
      • League\Csv\ByteSequence interface to decoupled BOM sequence from CSV documents
    • Improved CSV records column count consistency on insertion
      • League\Csv\ColumnConsistency
    • Improved CSV document flush mechanism on insertion
      • League\Csv\Writer::setFlushThreshold
      • League\Csv\Writer::getFlushThreshold
    • Improve RFC4180 compliance
      • League\Csv\RFC4180Field to format field according to RFC4180 rules

    Deprecated

    • Nothing

    Fixed

    • Improved CSV record insertion

      • League\Csv\Writer::insertOne only accepts an array and returns a integer
      • League\Csv\Writer::insertAll only accepts an iterable of array and returns an integer
    • Normalized CSV offset returned value

      • League\Csv\Reader::fetchColumn always returns the CSV document original offset.

    Removed

    • examples directory
    • PHP5/HHVM support
    • The following method is removed because The BOM sequence is now automatically stripped:
      • League\Csv\AbstractCsv::stripBOM
    • All conversion methods are removed in favor of conversion classes:
      • League\Csv\Writer::jsonSerialize
      • League\Csv\AbstractCsv::toHTML
      • League\Csv\AbstractCsv::toXML
      • League\Csv\AbstractCsv::setInputEncoding
      • League\Csv\AbstractCsv::getInputEncoding
    • The following methods are removed because the PHP stream filter API is simplified:
      • League\Csv\AbstractCsv::isActiveStreamFilter
      • League\Csv\AbstractCsv::setStreamFilterMode
      • League\Csv\AbstractCsv::appendStreamFilter
      • League\Csv\AbstractCsv::prependStreamFilter
      • League\Csv\AbstractCsv::removeStreamFilter
      • League\Csv\AbstractCsv::clearStreamFilters
    • The following methods are removed because switching between connections is no longer possible:
      • League\Csv\AbstractCsv::newReader
      • League\Csv\AbstractCsv::newWriter
      • League\Csv\Reader::getNewline
      • League\Csv\Reader::setNewline
    • The Exception mechanism is improved thus the following class is removed:
      • League\Csv\Exception\InvalidRowException;
    • The CSV records filtering methods are removed in favor of the League\Csv\Statement class:
      • League\Csv\AbstractCsv::addFilter,
      • League\Csv\AbstractCsv::addSortBy,
      • League\Csv\AbstractCsv::setOffset,
      • League\Csv\AbstractCsv::setLimit;
    • The following methods are removed as CSV records selection is simplified:
      • League\Csv\Reader::each
      • League\Csv\Reader::fetch
      • League\Csv\Reader::fetchAll
      • League\Csv\Reader::fetchAssoc
      • League\Csv\Reader::fetchPairsWithoutDuplicates
    • Formatting and validating CSV records on insertion is simplified, the following methods are removed:
      • League\Csv\Writer::hasFormatter
      • League\Csv\Writer::removeFormatter
      • League\Csv\Writer::clearFormatters
      • League\Csv\Writer::hasValidator
      • League\Csv\Writer::removeValidator
      • League\Csv\Writer::clearValidators
    • The following Formatters and Validators classes are removed from the package:
      • League\Csv\Plugin\SkipNullValuesFormatter
      • League\Csv\Plugin\ForbiddenNullValuesValidator
      • League\Csv\Plugin\ColumnConsistencyValidator replace by League\Csv\ColumnConsistency
    • League\Csv\Writer no longers implements the IteratorAggregate interface
    • League\Csv\AbstractCsv::fetchDelimitersOccurrence is removed replace by League\Csv\delimiter_detect function
    Source code(tar.gz)
    Source code(zip)
  • 8.2.2(Jul 12, 2017)

    Added

    • None

    Deprecated

    • None

    Fixed

    • Writer::insertOne was silently failing when inserting record in a CSV document in non-writing mode.
    • bug fix docblock

    Removed

    • None
    Source code(tar.gz)
    Source code(zip)
  • 8.2.1(Feb 23, 2017)

  • 8.2.0(Jan 25, 2017)

    Added

    • AbstractCsv::createFromStream to enable working with resource stream issue #202

    Deprecated

    • League\Csv\AbstractCsv::stripBom
    • League\Csv\Reader::getOffset
    • League\Csv\Reader::getLimit
    • League\Csv\Reader::getSortBy
    • League\Csv\Reader::getFilter
    • League\Csv\Reader::setOffset
    • League\Csv\Reader::setLimit
    • League\Csv\Reader::addSortBy
    • League\Csv\Reader::addFilter
    • League\Csv\Reader::fetch
    • League\Csv\Reader::each
    • League\Csv\Reader::fetchPairsWithoutDuplicates
    • League\Csv\Reader::fetchAssoc
    • League\Csv\Writer::removeFormatter
    • League\Csv\Writer::hasFormatter
    • League\Csv\Writer::clearFormatters
    • League\Csv\Writer::removeValidator
    • League\Csv\Writer::hasValidator
    • League\Csv\Writer::clearValidators
    • League\Csv\Writer::jsonSerialize
    • League\Csv\Writer::toHTML
    • League\Csv\Writer::toXML

    Fixed

    • None

    Removed

    • None
    Source code(tar.gz)
    Source code(zip)
  • 8.1.2(Oct 27, 2016)

  • 8.1.1(Sep 5, 2016)

  • 8.1.0(May 30, 2016)

    Added

    • The package now includes its own autoloader.
    • Ouput::getInputEncoding
    • Ouput::setInputEncoding

    Deprecated

    • Ouput::getEncodingFrom replaced by Ouput::getInputEncoding
    • Ouput::setEncodingFrom replaced by Ouput::setInputEncoding

    Fixed

    • Stream Filters are now url encoded before usage issue #72
    • All internal parameters are now using the snake case format
    Source code(tar.gz)
    Source code(zip)
  • 8.0.0(Dec 11, 2015)

    Added

    • Reader::fetchPairs
    • Reader::fetchPairsWithoutDuplicates

    Deprecated

    • None

    Fixed

    • Reader::fetchColumn and Reader::fetchAssoc now return Iterator
    • Reader::fetchAssoc callable argument expects an indexed row using the submitted keys as its first argument
    • Reader::fetchColumn callable argument expects the selected column value as its first argument
    • Default value on setOutputBOM is removed
    • AbstractCsv::getOutputBOM always return a string
    • AbstractCsv::getInputBOM always return a string

    Removed

    • Controls::setFlags
    • Controls::getFlags
    • Controls::detectDelimiterList
    • QueryFilter::removeFilter
    • QueryFilter::removeSortBy
    • QueryFilter::hasFilter
    • QueryFilter::hasSortBy
    • QueryFilter::clearFilter
    • QueryFilter::clearSortBy
    • Reader::query
    • The $newline argument from AbstractCsv::createFromString

    Please refers to the documentation or the library CHANGELOG for more details and a complete list of changes

    Source code(tar.gz)
    Source code(zip)
Owner
The League of Extraordinary Packages
A group of developers who have banded together to build solid, well tested PHP packages using modern coding standards.
The League of Extraordinary Packages
FIle Uploader is a php package to aid fast , easy and safe file upload

FILE UPLOADER FIle Uploader is a php package to aid fast , easy and safe file upload installation composer require codad5/file-helper Features Fast an

Aniezeofor Chibueze Michael 2 Sep 3, 2022
☁️ Nextcloud server, a safe home for all your data

Nextcloud Server ☁ A safe home for all your data. Why is this so awesome? ?? ?? Access your Data You can store your files, contacts, calendars and mor

Nextcloud 21.1k Dec 31, 2022
An object oriented PHP driver for FFMpeg binary

php-ffmpeg An Object-Oriented library to convert video/audio files with FFmpeg / AVConv. Check another amazing repo: PHP FFMpeg extras, you will find

null 4.4k Jan 2, 2023
PHP library that provides a filesystem abstraction layer − will be a feast for your files!

Gaufrette Gaufrette provides a filesystem abstraction layer. Why use Gaufrette? Imagine you have to manage a lot of medias in a PHP project. Lets see

KNP Labs 2.4k Jan 7, 2023
PHP-based anti-virus anti-trojan anti-malware solution.

What is phpMussel? An ideal solution for shared hosting environments, where it's often not possible to utilise or install conventional anti-virus prot

null 384 Dec 13, 2022
An object oriented PHP driver for FFMpeg binary

php-ffmpeg An Object-Oriented library to convert video/audio files with FFmpeg / AVConv. Check another amazing repo: PHP FFMpeg extras, you will find

null 4.4k Jan 2, 2023
PHP runtime & extensions header files for PhpStorm

phpstorm-stubs STUBS are normal, syntactically correct PHP files that contain function & class signatures, constant definitions, etc. for all built-in

JetBrains 1.2k Dec 25, 2022
PHP Phar Stream Wrapper

Based on Sam Thomas' findings concerning insecure deserialization in combination with obfuscation strategies allowing to hide Phar files inside valid image resources, the TYPO3 project decided back then to introduce a PharStreamWrapper to intercept invocations of the phar:// stream in PHP and only allow usage for defined locations in the file system.

TYPO3 GitHub Department 55 Dec 7, 2022
A PHP library to deal with all those media services around, parsing their URLs and displaying their audios/videos.

MediaEmbed A utility library that generates HTML embed tags for audio or video located on a given URL. It also parses and validates given media URLs.

Mark Sch. 165 Nov 10, 2022
Watch changes in the file system using PHP

Watch changes in the file system using PHP This package allows you to react to all kinds of changes in the file system. Here's how you can run code wh

Spatie 170 Dec 25, 2022