PHPackages                             otzy/csv-converter - 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. otzy/csv-converter

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

otzy/csv-converter
==================

saves source CSV file into target file with desired field set

v1.0.1(7y ago)011MITPHP

Since Jul 16Pushed 7y ago1 watchersCompare

[ Source](https://github.com/otzy/csv-converter)[ Packagist](https://packagist.org/packages/otzy/csv-converter)[ RSS](/packages/otzy-csv-converter/feed)WikiDiscussions master Synced yesterday

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

CSV Converter
=============

[](#csv-converter)

Transforms CSV file (or any plain text file with some delimiter) into a file with different set of fields.

Features

- Source and destination files may or may not have a header line
- Fields can be denoted by field name or by index
- Destination fields can be calculated using arbitrary callback functions
- Simple validity check of the source file header and data rows. CSV Converter checks that the header has exactly the same number and the order of fields that you expect.

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

[](#installation)

```
composer require otzy/csv-converter

```

CsvConverter class
------------------

[](#csvconverter-class)

To read source CSV and write to destination CSV the class uses league/csv Reader and Writer. Thus you can use reach functionality of league/csv package when you need to read/write using different encodings, BOM, stream filters, etc.

#### Mapping

[](#mapping)

To convert you should first create mapping.

Mapping is an associative array that defines which fields from the source file will be saved to the destination file. Keys in this array are field names of destination CSV and values are the corresponding fields from the source SCV.

Instead of field name you cn use callback to perform any transformation on input field.

This is an example of simple mapper:

```
$mapping = [
    'field two' => 'two',
    'field one' => 'one',
    'concat' => function ($fields, $source_field_index) {
        return $fields[$source_field_index['one']] . $fields[$source_field_index['two']];
    },
]
```

The mapper above will output 3 fields to the destination: "field two","field one","concat"

the third field will be concatenated from fields one and two from the source file.

#### CSVConverter usage example

[](#csvconverter-usage-example)

```
        $mapping = [
            'field two' => 'two',
            'field one' => 'one',
            'concat' => function ($fields, $source_field_index) {
                return $fields[$source_field_index['one']] . $fields[$source_field_index['two']];
            },
        ];

        // Create an instance of class and set mapping
        $converter = new CsvConverter();
        $converter->setMapping($mapping);

        // whether source CSV has a header line
        $converter->setSourceHasHeader(true);

        // whether we want the destination CSV to have header line
        $converter->setTargetHasHeader(true);

        // Call this method if you want to be sure that source file has specific fields
        $converter->setValidSourceHeader(['one', 'two', 'three']);

        $reader = Reader::createFromPath('source.csv');
        $reader->setDelimiter(';');
        $reader->setEscape('\\');
        $reader->setEnclosure('"');

        $writer = Writer::createFromPath('target.csv');

        $writer->setDelimiter(';');
        $writer->setEscape('\\');
        $writer->setEnclosure('"');

        $converter->convert($reader, $writer);

        // league/csv Writer does not close stream, the only way to do this is to unset the object
        unset($writer);
```

#### Events

[](#events)

Convert emits 3 types of events

- BeforeConvert - after the line from source has been read and before it's converted to the destination
- AfterRowConverted - after the converted row has been output to the destination stream
- Complete - when everything is done

You can use events for example for logging or to show the progress of conversion.

BeforeConvert can be used also to filter rows. If event handler returned false the row will not be written to the destination stream.

Event handlers can be set using the following functions:

public function onBeforeConvert($callback) public function onRowConverted($callback) public function onCompleted($callback)

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity65

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

Total

4

Last Release

2856d ago

Major Versions

v0.0.2 → v1.0.02018-07-18

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/11853333?v=4)[Evgeny Mazovetskiy](/maintainers/otzy)[@otzy](https://github.com/otzy)

---

Top Contributors

[![otzy](https://avatars.githubusercontent.com/u/11853333?v=4)](https://github.com/otzy "otzy (7 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/otzy-csv-converter/health.svg)

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

###  Alternatives

[grumpydictator/firefly-iii

Firefly III: a personal finances manager.

22.8k69.3k](/packages/grumpydictator-firefly-iii)[civicrm/civicrm-core

Open source constituent relationship management for non-profits, NGOs and advocacy organizations.

728272.9k20](/packages/civicrm-civicrm-core)[shlinkio/shlink

A self-hosted and PHP-based URL shortener application with CLI and REST interfaces

4.8k4.3k](/packages/shlinkio-shlink)[craftcms/feed-me

Import content from XML, RSS, CSV or JSON feeds into entries, categories, Craft Commerce products, and more.

292927.5k22](/packages/craftcms-feed-me)[troydavisson/phrets

RETS library in PHP

463355.6k](/packages/troydavisson-phrets)[firefly-iii/data-importer

Firefly III Data Import Tool.

7545.8k](/packages/firefly-iii-data-importer)

PHPackages © 2026

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