PHPackages                             htmlburger/carbon-csv - 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. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. htmlburger/carbon-csv

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

htmlburger/carbon-csv
=====================

Simple CSV file parser

1.0.2(8y ago)841.0k↓72.1%5[4 issues](https://github.com/htmlburger/carbon-csv/issues)[1 PRs](https://github.com/htmlburger/carbon-csv/pulls)PHPPHP ^5.4.0 || ^7.0

Since Sep 8Pushed 2y ago6 watchersCompare

[ Source](https://github.com/htmlburger/carbon-csv)[ Packagist](https://packagist.org/packages/htmlburger/carbon-csv)[ RSS](/packages/htmlburger-carbon-csv/feed)WikiDiscussions master Synced today

READMEChangelog (3)Dependencies (3)Versions (5)Used By (0)

Carbon CSV
==========

[](#carbon-csv)

Carbon CSV is a PHP library aimed at simplifying CSV parsing.

It provides simple interface to ease mapping columns via a header row, or custom column names.

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

[](#installation)

```
composer require htmlburger/carbon-csv
```

Usage
-----

[](#usage)

Suppose that you have the following CSV:

First NameLast NameCompany NameAddressHomerSimpsonSpringfield Nuclear Power Plant742 Evergreen Terrace, SpringfieldNedFlandersThe Leftorium744 Evergreen Terrace, SpringfieldHere is how you could iterate through the rows:

```
use \Carbon_CSV\CsvFile;
use \Carbon_CSV\Exception as CsvException;

try {
    $csv = new CsvFile('path-to-file/filename.csv');
    $csv->use_first_row_as_header();

    foreach ($csv as $row) {
        print_r($row);
    }
} catch (CsvException $e) {
    exit("Couldn't parse CSV file: " . $e->getMessage());
}
```

Would produce the following output:

```
Array
(
    [First Name] => Homer
    [Last Name] => Simpson
    [Company Name] => Springfield Nuclear Power Plant
    [Address] => 742 Evergreen Terrace, Springfield
)
Array
(
    [First Name] => Ned
    [Last Name] => Flanders
    [Company Name] => The Leftorium
    [Address] => 744 Evergreen Terrace, Springfield
)

```

Alternatively, you could also provide your own column names:

```
use \Carbon_CSV\CsvFile;
use \Carbon_CSV\Exception as CsvException;

try {
    $csv = new CsvFile('path-to-file/filename.csv');
    $csv->use_first_row_as_header();
    $csv->set_column_names([
        'First Name'   => 'fname',
        'Last Name'    => 'lname',
        'Company Name' => 'company',
        'Address'      => 'address',
    ]);

    foreach ($csv as $row) {
        print_r($row);
    }
} catch (CsvException $e) {
    exit("Couldn't parse CSV file: " . $e->getMessage());
}
```

Would produce the following output:

```
Array
(
    [fname] => Homer
    [lname] => Simpson
    [company] => Springfield Nuclear Power Plant
    [address] => 742 Evergreen Terrace, Springfield
)
Array
(
    [fname] => Ned
    [lname] => Flanders
    [company] => The Leftorium
    [address] => 744 Evergreen Terrace, Springfield
)

```

**MacOS encoding**

When working with files created on a Mac device, you should set the `auto_detect_line_endings` PHP variable to `1`.

```
ini_set( 'auto_detect_line_endings', 1 );

```

### Settings

[](#settings)

To change the delimiter, enclosure and escape characters for the CSV file, simply pass them as arguments after the file path.

Example:

```
use \Carbon_CSV\CsvFile as CsvFile;
use \Carbon_CSV\Exception;

$csv = new CsvFile('path-to-file/filename.csv', ';', '|', '/');
$rows = $csv->to_array();
```

### Methods

[](#methods)

Methods for skipping rows or columns work with zero based indexes.

#### `skip_to_row(int $row_index)`

[](#skip_to_rowint-row_index)

To skip to a specific row, simply pass the index of the row.

This will tell the parser to start reading from that row until the end of the file.

```
use \Carbon_CSV\CsvFile as CsvFile;
use \Carbon_CSV\Exception;

$csv = new CsvFile('path-to-file/filename.csv');
$csv->skip_to_row(1);
$rows = $csv->to_array();
```

Contents before skipping to a specific row:

```
Array
(
    [0] => Array
        (
            [0] => John
            [1] => Doe
            [2] => Simple Company Name
            [3] => Street Name, 1234, City Name, Country Name
        )
    [1] => Array
        (
            [0] => Jane
            [1] => Doe
            [2] => Nice Company Name
            [3] => Street Name, 5678, City Name, Country Name
        )
)

```

Contents after skipping to a specific row:

```
Array
(
    [0] => Array
        (
            [0] => Jane
            [1] => Doe
            [2] => Nice Company Name
            [3] => Street Name, 5678, City Name, Country Name
        )
)

```

#### `skip_to_column(int $col_index)`

[](#skip_to_columnint-col_index)

To skip to a specific column, simply pass the index of the column.

```
use \Carbon_CSV\CsvFile as CsvFile;
use \Carbon_CSV\Exception;

$csv = new CsvFile('path-to-file/filename.csv');
$csv->skip_to_column(2);
$rows = $csv->to_array();
```

Contents before skipping to a specific column:

```
Array
(
    [0] => Array
        (
            [0] => John
            [1] => Doe
            [2] => Simple Company Name
            [3] => Street Name, 1234, City Name, Country Name
        )
    [1] => Array
        (
            [0] => Jane
            [1] => Doe
            [2] => Nice Company Name
            [3] => Street Name, 5678, City Name, Country Name
        )
)

```

Contents after skipping to a specific column:

```
Array
(
    [0] => Array
        (
            [0] => Simple Company Name
            [1] => Street Name, 1234, City Name, Country Name
        )
    [1] => Array
        (
            [0] => Nice Company Name
            [1] => Street Name, 5678, City Name, Country Name
        )
)

```

#### `skip_columns(array $col_indexes)`

[](#skip_columnsarray-col_indexes)

To skip multiple columns, pass the indexes of those columns as an array.

```
use \Carbon_CSV\CsvFile as CsvFile;
use \Carbon_CSV\Exception;

$csv = new CsvFile('path-to-file/filename.csv');
$csv->skip_columns(array(0, 2, 3));
$rows = $csv->to_array();
```

Contents before skipping columns:

```
Array
(
    [0] => Array
        (
            [0] => John
            [1] => Doe
            [2] => Simple Company Name
            [3] => Street Name, 1234, City Name, Country Name
        )
    [1] => Array
        (
            [0] => Jane
            [1] => Doe
            [2] => Nice Company Name
            [3] => Street Name, 5678, City Name, Country Name
        )
)

```

Contents after skipping columns:

```
Array
(
    [0] => Array
        (
            [0] => Doe
        )
    [1] => Array
        (
            [0] => Doe
        )
)

```

#### `use_first_row_as_header()`

[](#use_first_row_as_header)

To use the first row from the CSV, simply call this method.

**Note:** if `skip_to_row` is called prior to calling `use_first_row_as_header`, the parser will use the new first row as a header.

```
use \Carbon_CSV\CsvFile as CsvFile;
use \Carbon_CSV\Exception;

$csv = new CsvFile('path-to-file/filename.csv');
$csv->use_first_row_as_header();
$rows = $csv->to_array();
```

Contents before assigning a header row:

```
Array
(
    [0] => Array
        (
            [0] => First Name
            [1] => Last Name
        )
    [1] => Array
        (
            [0] => John
            [1] => Doe
        )
    [2] => Array
        (
            [0] => Jane
            [1] => Dove
        )
)

```

Contents after assigning a header row:

```
Array
(
    [0] => Array
        (
            [First Name] => John
            [Last Name] => Doe
        )
    [1] => Array
        (
            [First Name] => Jane
            [Last Name] => Dove
        )
)

```

Since we're telling the parser to use the first row as a header row, it is assigned and skipped.

#### `set_column_names(array $columns_mapping)`

[](#set_column_namesarray-columns_mapping)

If you wish to use your own indexes for the columns, pass them using an array.

**Note:** you can use `set_column_names` in conjunction with `use_first_row_as_header`, so you can set the names of the columns based on the header row.

Example without `use_first_row_as_header` (using a file without a head row):

```
use \Carbon_CSV\CsvFile as CsvFile;
use \Carbon_CSV\Exception;

$csv = new CsvFile('path-to-file/filename-no-head-rows.csv');
$csv->set_column_names([
    0 => 'first_name',
    1 => 'last_name',
    2 => 'company_name',
    3 => 'address',
]);
$rows = $csv->to_array();
```

Contents before setting custom column names:

```
Array
(
    [0] => Array
        (
            [0] => John
            [1] => Doe
            [2] => Simple Company Name
            [3] => Street Name, 1234, City Name, Country Name
        )
    [1] => Array
        (
            [0] => Jane
            [1] => Doe
            [2] => Nice Company Name
            [3] => Street Name, 5678, City Name, Country Name
        )
)

```

Contents after setting custom column names:

```
Array
(
    [0] => Array
        (
            [first_name] => John
            [last_name] => Doe
            [company_name] => Simple Company Name
            [address] => Street Name, 1234, City Name, Country Name
        )
    [1] => Array
        (
            [first_name] => Jane
            [last_name] => Doe
            [company_name] => Nice Company Name
            [address] => Street Name, 5678, City Name, Country Name
        )
)

```

---

Example with `use_first_row_as_header` (using a file with a head row):

```
use \Carbon_CSV\CsvFile as CsvFile;
use \Carbon_CSV\Exception;

$csv = new CsvFile('path-to-file/filename-no-head-rows.csv');
$csv->use_first_row_as_header();
$csv->set_column_names([
    'First Name' => 'first_name',
    'Last Name' => 'last_name',
    'Company Name' => 'company_name',
    'Address' => 'address',
]);
$rows = $csv->to_array();
```

Contents before setting custom column names:

```
Array
(
    [0] => Array
        (
            [0] => First Name
            [1] => Last Name
            [2] => Company Name
            [3] => Address
        )
    [1] => Array
        (
            [0] => John
            [1] => Doe
            [2] => Simple Company Name
            [3] => Street Name, 1234, City Name, Country Name
        )
    [2] => Array
        (
            [0] => Jane
            [1] => Doe
            [2] => Nice Company Name
            [3] => Street Name, 5678, City Name, Country Name
        )
)

```

Contents after setting custom column names:

```
Array
(
    [0] => Array
        (
            [first_name] => John
            [last_name] => Doe
            [company_name] => Simple Company Name
            [address] => Street Name, 1234, City Name, Country Name
        )
    [1] => Array
        (
            [first_name] => Jane
            [last_name] => Doe
            [company_name] => Nice Company Name
            [address] => Street Name, 5678, City Name, Country Name
        )
)

```

#### `set_encoding($encoding)`

[](#set_encodingencoding)

Set the encoding of the CSV file. This is needed, so it can be properly converted to utf-8

Example:

```
use \Carbon_CSV\CsvFile as CsvFile;
use \Carbon_CSV\Exception;

$csv = new CsvFile('path-to-file/filename.csv');
$csv->set_encoding('windows-1251');
$total_number_of_rows = $csv->count();
```

#### `count()`

[](#count)

Get the total number of rows in the CSV file (this skips the empty rows):

```
use \Carbon_CSV\CsvFile as CsvFile;
use \Carbon_CSV\Exception;

$csv = new CsvFile('path-to-file/filename.csv');
$total_number_of_rows = $csv->count();
```

`$total_number_of_rows = $csv->count()` is equivalent to `count($csv->to_array())`.

###  Health Score

33

—

LowBetter than 72% of packages

Maintenance10

Infrequent updates — may be unmaintained

Popularity33

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

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 ~121 days

Total

3

Last Release

2978d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/1150772?v=4)[HTMLBurger](/maintainers/htmlburger-git)[@htmlburger-git](https://github.com/htmlburger-git)

---

Top Contributors

[![emohamed](https://avatars.githubusercontent.com/u/536840?v=4)](https://github.com/emohamed "emohamed (9 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/htmlburger-carbon-csv/health.svg)

```
[![Health](https://phpackages.com/badges/htmlburger-carbon-csv/health.svg)](https://phpackages.com/packages/htmlburger-carbon-csv)
```

###  Alternatives

[mck89/peast

Peast is PHP library that generates AST for JavaScript code

19139.2M47](/packages/mck89-peast)[sauladam/shipment-tracker

Parses tracking information for several carriers, like UPS, USPS, DHL and GLS by simply scraping the data. No need for any kind of API access.

9843.5k](/packages/sauladam-shipment-tracker)[jstewmc/rtf

Read and write Rich Text Format (RTF) documents with PHP

45153.1k6](/packages/jstewmc-rtf)[tcds-io/php-jackson

A lightweight, flexible object serializer for PHP, inspired by FasterXML/jackson

113.2k10](/packages/tcds-io-php-jackson)

PHPackages © 2026

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