PHPackages                             parsecsv/php-parsecsv - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. parsecsv/php-parsecsv

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

parsecsv/php-parsecsv
=====================

CSV data parser for PHP

1.3.2(4y ago)6885.7M—4.9%173[8 issues](https://github.com/parsecsv/parsecsv-for-php/issues)20MITPHPPHP &gt;=5.5CI passing

Since Jun 3Pushed 3mo ago34 watchersCompare

[ Source](https://github.com/parsecsv/parsecsv-for-php)[ Packagist](https://packagist.org/packages/parsecsv/php-parsecsv)[ RSS](/packages/parsecsv-php-parsecsv/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (9)Dependencies (2)Versions (13)Used By (20)

ParseCsv
========

[](#parsecsv)

[![PHPUnit](https://github.com/parsecsv/parsecsv-for-php/actions/workflows/phpunit.yml/badge.svg)](https://github.com/parsecsv/parsecsv-for-php/actions/workflows/phpunit.yml/badge.svg)

ParseCsv is an easy-to-use PHP class that reads and writes CSV data properly. It fully conforms to the specifications outlined on the [Wikipedia article](http://en.wikipedia.org/wiki/Comma-separated_values) (and thus RFC 4180). It has many advanced features which help make your life easier when dealing with CSV data.

You may not need a library at all: before using ParseCsv, please make sure if PHP's own `str_getcsv()`, `fgetcsv()` or `fputcsv()` meets your needs.

This library was originally created in early 2007 by [jimeh](https://github.com/jimeh) due to the lack of built-in and third-party support for handling CSV data in PHP.

Features
--------

[](#features)

- ParseCsv is a complete and fully featured CSV solution for PHP
- Supports enclosed values, enclosed commas, double quotes and new lines.
- Automatic delimiter character detection.
- Sort data by specific fields/columns.
- Easy data manipulation.
- Basic SQL-like *conditions*, *offset* and *limit* options for filtering data.
- Error detection for incorrectly formatted input. It attempts to be intelligent, but can not be trusted 100% due to the structure of CSV, and how different programs like Excel for example outputs CSV data.
- Support for character encoding conversion using PHP's `iconv()` and `mb_convert_encoding()` functions.
- Supports PHP 5.5 and higher. It certainly works with PHP 8.3 and all versions in between.

Installation
------------

[](#installation)

Installation is easy using Composer. Just run the following on the command line:

```
composer require parsecsv/php-parsecsv

```

If you don't use a framework such as Drupal, Laravel, Symfony, Yii etc., you may have to manually include Composer's autoloader file in your PHP script:

```
require_once __DIR__ . '/vendor/autoload.php';
```

#### Without composer

[](#without-composer)

Not recommended, but technically possible: you can also clone the repository or extract the [ZIP](https://github.com/parsecsv/parsecsv-for-php/archive/master.zip). To use ParseCSV, you then have to add a `require 'parsecsv.lib.php';` line.

Example Usage
-------------

[](#example-usage)

**Parse a tab-delimited CSV file with encoding conversion**

```
$csv = new \ParseCsv\Csv();
$csv->encoding('UTF-16', 'UTF-8');
$csv->delimiter = "\t";
$csv->parseFile('data.tsv');
print_r($csv->data);
```

**Auto-detect field delimiter character**

```
$csv = new \ParseCsv\Csv();
$csv->auto('data.csv');
print_r($csv->data);
```

**Parse data with offset**

- ignoring the first X (e.g. two) rows

```
$csv = new \ParseCsv\Csv();
$csv->offset = 2;
$csv->parseFile('data.csv');
print_r($csv->data);
```

**Limit the number of returned data rows**

```
$csv = new \ParseCsv\Csv();
$csv->limit = 5;
$csv->parseFile('data.csv');
print_r($csv->data);
```

**Get total number of data rows without parsing whole data**

- Excluding heading line if present (see $csv-&gt;header property)

```
$csv = new \ParseCsv\Csv();
$csv->loadFile('data.csv');
$count = $csv->getTotalDataRowCount();
print_r($count);
```

**Get most common data type for each column**

```
$csv = new \ParseCsv\Csv('data.csv');
$csv->getDatatypes();
print_r($csv->data_types);
```

**Modify data in a CSV file**

Change data values:

```
$csv = new \ParseCsv\Csv();
$csv->sort_by = 'id';
$csv->parseFile('data.csv');
# "4" is the value of the "id" column of the CSV row
$csv->data[4] = array('firstname' => 'John', 'lastname' => 'Doe', 'email' => 'john@doe.com');
$csv->save();
```

Enclose each data value by quotes:

```
$csv = new \ParseCsv\Csv();
$csv->parseFile('data.csv');
$csv->enclose_all = true;
$csv->save();
```

**Replace field names or set ones if missing**

```
$csv = new \ParseCsv\Csv();
$csv->fields = ['id', 'name', 'category'];
$csv->parseFile('data.csv');
```

**Add row/entry to end of CSV file**

*Only recommended when you know the exact structure of the file.*

```
$csv = new \ParseCsv\Csv();
$csv->save('data.csv', array(array('1986', 'Home', 'Nowhere', '')), /* append */ true);
```

**Convert 2D array to CSV data and send headers to browser to treat output as a file and download it**

Your web app users would call this an export.

```
$csv = new \ParseCsv\Csv();
$csv->linefeed = "\n";
$header = array('field 1', 'field 2');
$csv->output('movies.csv', $data_array, $header, ',');
```

For more complex examples, see the `tests` and `examples` directories.

Test coverage
-------------

[](#test-coverage)

All tests are located in the `tests` directory. To execute tests, run the following commands:

```
composer install
composer run test
```

Note that PHP 8.2 and newer allow PHPUnit versions that deprecate `@annotations`. The GitHub actions use Rector to convert them to `#[attributes]`. When pushing code to GitHub, tests will be executed using GitHub Actions. The relevant configuration is in the file `.github/workflows/ci.yml`. To run the `test` action locally, you can execute the following command:

```
make local-ci
```

Security
--------

[](#security)

If you discover any security related issues, please email  instead of using GitHub issues.

Credits
-------

[](#credits)

- ParseCsv is based on the concept of [Ming Hong Ng](http://minghong.blogspot.com/)'s [CsvFileParser](http://minghong.blogspot.com/2006/07/csv-parser-for-php.html)class.

Contributors
------------

[](#contributors)

### Code Contributors

[](#code-contributors)

This project exists thanks to all the people who contribute.

Please find a complete list on the project's [contributors](https://github.com/parsecsv/parsecsv-for-php/graphs/contributors) page.

License
-------

[](#license)

(The MIT license)

Copyright (c) 2014 Jim Myhrberg.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

[![Build Status](https://camo.githubusercontent.com/8f8421a4fcd51a46ecad0ad4b6e16f3e00a301c5728c61f2e14a157b053ae44b/68747470733a2f2f7472617669732d63692e6f72672f70617273656373762f70617273656373762d666f722d7068702e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/parsecsv/parsecsv-for-php)

###  Health Score

61

—

FairBetter than 99% of packages

Maintenance54

Moderate activity, may be stable

Popularity68

Solid adoption and visibility

Community48

Growing community involvement

Maturity65

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~339 days

Recently: every ~167 days

Total

9

Last Release

1653d ago

Major Versions

0.4.5 → 1.0.02018-03-03

### Community

Maintainers

![](https://www.gravatar.com/avatar/335890ed9e0dee70106593177c8daaadb4485a6df66e7a53c5e822f2a83c1009?d=identicon)[williamknauss](/maintainers/williamknauss)

![](https://www.gravatar.com/avatar/c8db1774f9273f74431b1b1e0bf9a42cd1c03629510403162b88f422f129f721?d=identicon)[parsecsv](/maintainers/parsecsv)

![](https://www.gravatar.com/avatar/69ebdd357f7113d29d9298b2e960c5b36fc8c5496dbfecdc5975e5197eea3d0b?d=identicon)[jimeh](/maintainers/jimeh)

![](https://avatars.githubusercontent.com/u/549369?v=4)[Fonata](/maintainers/Fonata)[@fonata](https://github.com/fonata)

![](https://www.gravatar.com/avatar/82c7330bdd40273ef94d3f23964e0b439b01ce4e1501a3c7fbe695e9661cc91a?d=identicon)[susgo](/maintainers/susgo)

---

Top Contributors

[![Gogowitsch](https://avatars.githubusercontent.com/u/6819464?v=4)](https://github.com/Gogowitsch "Gogowitsch (114 commits)")[![susgo](https://avatars.githubusercontent.com/u/20994932?v=4)](https://github.com/susgo "susgo (66 commits)")[![waknauss](https://avatars.githubusercontent.com/u/20912626?v=4)](https://github.com/waknauss "waknauss (37 commits)")[![jimeh](https://avatars.githubusercontent.com/u/39563?v=4)](https://github.com/jimeh "jimeh (20 commits)")[![fonata](https://avatars.githubusercontent.com/u/549369?v=4)](https://github.com/fonata "fonata (19 commits)")[![Norcoen](https://avatars.githubusercontent.com/u/1840514?v=4)](https://github.com/Norcoen "Norcoen (4 commits)")[![Piskvor](https://avatars.githubusercontent.com/u/917239?v=4)](https://github.com/Piskvor "Piskvor (2 commits)")[![repat](https://avatars.githubusercontent.com/u/516807?v=4)](https://github.com/repat "repat (2 commits)")[![SharkMachine](https://avatars.githubusercontent.com/u/7488694?v=4)](https://github.com/SharkMachine "SharkMachine (2 commits)")[![tunecino](https://avatars.githubusercontent.com/u/5133397?v=4)](https://github.com/tunecino "tunecino (1 commits)")[![morozov](https://avatars.githubusercontent.com/u/59683?v=4)](https://github.com/morozov "morozov (1 commits)")[![BreyndotEchse](https://avatars.githubusercontent.com/u/2163881?v=4)](https://github.com/BreyndotEchse "BreyndotEchse (1 commits)")[![geminorum](https://avatars.githubusercontent.com/u/150718?v=4)](https://github.com/geminorum "geminorum (1 commits)")[![helpse](https://avatars.githubusercontent.com/u/1523409?v=4)](https://github.com/helpse "helpse (1 commits)")[![lbajsarowicz](https://avatars.githubusercontent.com/u/1639941?v=4)](https://github.com/lbajsarowicz "lbajsarowicz (1 commits)")[![monkeywithacupcake](https://avatars.githubusercontent.com/u/7316730?v=4)](https://github.com/monkeywithacupcake "monkeywithacupcake (1 commits)")[![andreybolonin](https://avatars.githubusercontent.com/u/2576509?v=4)](https://github.com/andreybolonin "andreybolonin (1 commits)")[![morrislaptop](https://avatars.githubusercontent.com/u/67807?v=4)](https://github.com/morrislaptop "morrislaptop (1 commits)")[![Mte90](https://avatars.githubusercontent.com/u/403283?v=4)](https://github.com/Mte90 "Mte90 (1 commits)")

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/parsecsv-php-parsecsv/health.svg)

```
[![Health](https://phpackages.com/badges/parsecsv-php-parsecsv/health.svg)](https://phpackages.com/packages/parsecsv-php-parsecsv)
```

###  Alternatives

[chalcedonyt/laravel-specification

Implementation of the specification pattern

128.6k](/packages/chalcedonyt-laravel-specification)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
