2017 © Pedro Peláez
 

library php-parsecsv

CSV data parser for PHP

image

parsecsv/php-parsecsv

CSV data parser for PHP

  • Tuesday, May 22, 2018
  • by williamknauss
  • Repository
  • 34 Watchers
  • 418 Stars
  • 626,648 Installations
  • PHP
  • 10 Dependents
  • 0 Suggesters
  • 151 Forks
  • 7 Open issues
  • 18 Versions
  • 13 % Grown

The README.md

ParseCsv

Financial Contributors on Open Collective, (*1)

ParseCsv is an easy-to-use PHP class that reads and writes CSV data properly. It fully conforms to the specifications outlined on the on the Wikipedia article (and thus RFC 4180). It has many advanced features which help make your life easier when dealing with CSV data., (*2)

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., (*3)

This library was originally created in early 2007 by jimeh due to the lack of built-in and third-party support for handling CSV data in PHP., (*4)

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 7.2 and all versions in between.

Installation

Installation is easy using Composer. Just run the following on the command line:, (*5)

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:, (*6)

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

Without composer

Not recommended, but technically possible: you can also clone the repository or extract the ZIP. To use ParseCSV, you then have to add a require 'parsecsv.lib.php'; line., (*7)

Example Usage

Parse a tab-delimited CSV file with encoding conversion, (*8)

$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, (*9)

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

Parse data with offset * ignoring the first X (e.g. two) rows, (*10)

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

Limit the number of returned data rows, (*11)

$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->header property), (*12)

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

Get most common data type for each column, (*13)

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

Modify data in a CSV file, (*14)

Change data values:, (*15)

$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:, (*16)

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

Replace field names or set ones if missing, (*17)

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

Add row/entry to end of CSV file, (*18)

Only recommended when you know the exact structure of the file., (*19)

$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, (*20)

Your web app users would call this an export., (*21)

$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., (*22)

Test coverage

All tests are located in the tests directory. To execute tests, run the following commands:, (*23)

composer install
composer run test

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:, (*24)

make local-ci

Security

If you discover any security related issues, please email ParseCsv@blaeul.de instead of using GitHub issues., (*25)

Credits

Contributors

Code Contributors

This project exists thanks to all the people who contribute., (*26)

Please find a complete list on the project's contributors page., (*27)

Financial Contributors

Become a financial contributor and help us sustain our community. [Contribute], (*28)

Individuals

, (*29)

Organizations

Support this project with your organization. Your logo will show up here with a link to your website. [Contribute], (*30)

, (*31)

License

(The MIT license), (*32)

Copyright (c) 2014 Jim Myhrberg., (*33)

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:, (*34)

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

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., (*36)

Build Status, (*37)

The Versions

22/05 2018

dev-master

9999999-dev

CSV data parser for PHP

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

by Jim Myhrberg
by William Knauss

21/05 2018

dev-waiver-test

dev-waiver-test

CSV data parser for PHP

  Sources   Download

MIT

The Requires

  • php >=5.5

 

The Development Requires

by Jim Myhrberg
by William Knauss

29/04 2018

dev-issue-141-multiple-empty-lines

dev-issue-141-multiple-empty-lines

CSV data parser for PHP

  Sources   Download

MIT

The Development Requires

by Jim Myhrberg
by William Knauss

28/03 2018

dev-feature-datacollections

dev-feature-datacollections

CSV data parser for PHP

  Sources   Download

MIT

The Development Requires

by Jim Myhrberg
by William Knauss

28/03 2018

dev-new-unparse-test

dev-new-unparse-test

CSV data parser for PHP

  Sources   Download

MIT

The Development Requires

by Jim Myhrberg
by William Knauss

17/03 2018

dev-fixing-132-output-with-fields

dev-fixing-132-output-with-fields

CSV data parser for PHP

  Sources   Download

MIT

The Development Requires

by Jim Myhrberg
by William Knauss

12/03 2018

dev-dropping-php-5.4

dev-dropping-php-5.4

CSV data parser for PHP

  Sources   Download

MIT

The Development Requires

by Jim Myhrberg
by William Knauss

03/03 2018

dev-testing-backwards-compatibility

dev-testing-backwards-compatibility

CSV data parser for PHP

  Sources   Download

MIT

The Development Requires

by Jim Myhrberg
by William Knauss

03/03 2018

1.0.0

1.0.0.0

CSV data parser for PHP

  Sources   Download

MIT

The Development Requires

by Jim Myhrberg
by William Knauss

02/03 2018

dev-offset-comment-and-tests

dev-offset-comment-and-tests

CSV data parser for PHP

  Sources   Download

MIT

The Development Requires

by Jim Myhrberg
by William Knauss

23/02 2018

dev-improved-compatibility-if-no-composer

dev-improved-compatibility-if-no-composer

CSV data parser for PHP

  Sources   Download

MIT

The Development Requires

by Jim Myhrberg
by William Knauss

23/02 2018

dev-Tests-for-sort-and-cond

dev-Tests-for-sort-and-cond

CSV data parser for PHP

  Sources   Download

MIT

The Development Requires

by Jim Myhrberg
by William Knauss

22/02 2018

dev-resolve-issue-79-mime-in-output

dev-resolve-issue-79-mime-in-output

CSV data parser for PHP

  Sources   Download

MIT

The Development Requires

by Jim Myhrberg
by William Knauss

21/01 2018

dev-triple-line-endings

dev-triple-line-endings

CSV data parser for PHP

  Sources   Download

MIT

The Development Requires

by Jim Myhrberg
by William Knauss

06/12 2017

dev-BOMness

dev-BOMness

CSV data parser for PHP

  Sources   Download

MIT

The Development Requires

by Jim Myhrberg
by William Knauss

25/11 2017

dev-Piskvor-master

dev-Piskvor-master

CSV data parser for PHP

  Sources   Download

MIT

The Development Requires

by Jim Myhrberg
by William Knauss

03/11 2017

dev-added-parse-test

dev-added-parse-test

CSV data parser for PHP

  Sources   Download

MIT

The Development Requires

by Jim Myhrberg
by William Knauss

03/06 2014

0.4.5

0.4.5.0

CSV data parser for PHP

  Sources   Download

MIT

The Development Requires

by Jim Myhrberg