2017 © Pedro Peláez
 

library php-csv-parser

Convert CSV to array (Excel style is fully suppoted!)

image

kzykhys/php-csv-parser

Convert CSV to array (Excel style is fully suppoted!)

  • Wednesday, January 15, 2014
  • by kzykhys
  • Repository
  • 7 Watchers
  • 51 Stars
  • 78,250 Installations
  • PHP
  • 4 Dependents
  • 1 Suggesters
  • 23 Forks
  • 5 Open issues
  • 10 Versions
  • 11 % Grown

The README.md

PHPCsvParser

Convert CSV to array/Iterator (Excel style is fully suppoted!), (*1)

Latest Stable Version Build Status Coverage Status SensioLabsInsight, (*2)

Why PHPCsvParser?

As you know, PHP has built-in fgetcsv function. But has some probrems:, (*3)

  • Line breaks in the cell
  • Multibyte string (especially NON UTF-8)
  • Double quote in the cell

Requirements

PHP5.3.3 or later, (*4)

Installation

Create or modify your composer.json, (*5)

``` json { "require": { "kzykhys/php-csv-parser": ">1.4" } }, (*6)


And run ``` sh $ php composer.phar install

Usage

Parse a CSV file

1,"some text",150
2,"some multi line
text",2000

``` php <?php, (*7)

require('./vendor/autoload.php');, (*8)

$parser = \KzykHys\CsvParser\CsvParser::fromFile('./test.csv'); $result = $parser->parse();, (*9)

var_dump($result);, (*10)


This is the same as: ``` php <?php require('./vendor/autoload.php'); $iterator = new \SplFileObject('./test.csv'); $parser = new \KzykHys\CsvParser\CsvParser($iterator); $result = $parser->parse(); var_dump($result);

Parse from string

``` php <?php, (*11)

require('./vendor/autoload.php');, (*12)

$parser = \KzykHys\CsvParser\CsvParser::fromString($string); $result = $parser->parse();, (*13)

var_dump($result);, (*14)


### Parse from array/Iterator ``` php <?php require('./vendor/autoload.php'); $parser = \KzykHys\CsvParser\CsvParser::fromArray(array('a,b,c,d', 'e,f,g,h')); $result = $parser->parse(); $iterator = new ArrayIterator(array('a,b,c,d', 'e,f,g,h')); $parser2 = new \KzykHys\CsvParser\CsvParser($iterator); $result2 = $parser2->parse(); var_dump($result); var_dump($result2);

Handling Large files

The class \KzykHys\CsvParser\CsvParser itself is Traversable. so You can convert CSV lines on-the-fly., (*15)

Following example is the best choice for performance:, (*16)

``` php <?php, (*17)

require('./vendor/autoload.php');, (*18)

$parser = \KzykHys\CsvParser\CsvParser::fromFile('./test.csv');, (*19)

foreach ($parser as $record) { // handles each record var_dump($record); } ```, (*20)

Options

You can pass the options to 2nd argument of each static methods., (*21)

  • CsvParser::fromFile($file, $options);
  • CsvParser::fromString($string, $options);
  • CsvParser::fromArray($array, $options);
  • new CsvParser($iterator, $options);

Available options are:, (*22)

Option Type Description Default
delimiter string The field delimiter (one character only) ,
enclosure string The field enclosure character (one character only) "
encoding string The type of encoding CP932
offset integer (>=0) The sequence will start at that offset 0
limit integer (>=-1) Limit maximum count of records -1 (unlimited)
header array or false Use the specified index instead of the column number false

Testing

Just run phpunit (PHPUnit is required), (*23)

Author

Kazuyuki Hayashi (@kzykhys), (*24)

Changelog

see CHANGELOG, (*25)

License

The MIT License, (*26)

The Versions

15/01 2014

dev-master

9999999-dev

Convert CSV to array (Excel style is fully suppoted!)

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

by Kazuyuki Hayashi

15/01 2014

1.4.x-dev

1.4.9999999.9999999-dev

Convert CSV to array (Excel style is fully suppoted!)

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

by Kazuyuki Hayashi

15/01 2014

1.4.1

1.4.1.0

Convert CSV to array (Excel style is fully suppoted!)

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

by Kazuyuki Hayashi

03/09 2013

1.4.0

1.4.0.0

Convert CSV to array (Excel style is fully suppoted!)

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

by Kazuyuki Hayashi

25/07 2013

1.3.0

1.3.0.0

Convert CSV to array (Excel style is fully suppoted!)

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

by Kazuyuki Hayashi

22/07 2013

1.2.2

1.2.2.0

Convert CSV to array (Excel style is fully suppoted!)

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

The Development Requires

by Kazuyuki Hayashi

22/07 2013

1.2.1

1.2.1.0

Convert CSV to array (Excel style is fully suppoted!)

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

by Kazuyuki Hayashi

22/03 2013

1.2.0

1.2.0.0

Convert CSV to array (Excel style is fully suppoted!)

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

by Kazuyuki Hayashi

12/03 2013

1.1.0

1.1.0.0

Convert CSV to array (Excel style is fully suppoted!)

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

by Kazuyuki Hayashi

31/01 2013

1.0.0

1.0.0.0

Convert CSV to array (Excel style is fully suppoted!)

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

by Kazuyuki Hayashi