2017 © Pedro Peláez
 

library csv-parser

Parse CSV data from a file, stream or string

image

jabranr/csv-parser

Parse CSV data from a file, stream or string

  • Friday, February 10, 2017
  • by jabranr
  • Repository
  • 1 Watchers
  • 9 Stars
  • 1,120 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 6 Forks
  • 6 Open issues
  • 10 Versions
  • 7 % Grown

The README.md

CSV Parser (PHP) Build Status Latest Stable Version Total Downloads

PHP client to parse CSV data from a path, file, stream, resource or string into indexed or associative arrays., (*1)

Namespaces were added in v2.0.2 so make sure you update your code before migrating from an older version., (*2)

Install

Install using composer, (*3)

#composer.json

{
  "require": {
    "jabranr/csv-parser": "~2.1.*"
  }
}

Run following to install, (*4)

$ comsposer install

Use

Initiate a new instance, (*5)

$csv = new Jabran\CSV_Parser();

API

Get data from a string, (*6)

/* @param: string $str */
$csv->fromString($str);

Get data from a stream (Will be deprecated in future), (*7)

/* @param: resource $stream (f.e. php://input) */
$csv->fromStream($stream);

Get data from a resource (Since v2.0.2), (*8)

/* @param: resource $resource (f.e. resource created using fopen()) */
$csv->fromResource($resource);

Get data from a file path (Will be deprecated in future), (*9)

/* @param: string $file */
$csv->fromFile($file);

Get data from a path/URL (Since v2.0.2), (*10)

/* @param: string $path */
$csv->fromPath($path);

Parse data for output, (*11)

/**
 * Set $headers true/false to include top/first row
 * and output an associative array
 *
 * @param: boolean $headers (Default: true)
 * @return: array
 */
$csv->parse($headers);

More useful methods (Since v2.0.2), (*12)

/**
 * Set columns
 * @param array $columns
 * @return Jabran\CSV_Parser
 */
$csv->setColumns($columns);

/**
 * Set rows
 * @param array $rows
 * @return Jabran\CSV_Parser
 */
$csv->setRows($rows);

/**
 * Set headers
 * @param array $headers
 * @return Jabran\CSV_Parser
 */
$csv->setHeaders($headers);

/**
 * Get columns
 * @return array
 */
$csv->getColumns();

/**
 * Get rows
 * @return array
 */
$csv->getRows();

/**
 * Get headers
 * @return array
 */
$csv->getHeaders();

Example

Example input string, (*13)

require 'path/to/vendor/autoload.php';

$csv = new Jabran\CSV_Parser;

$str = 'id,first_name,last_name;1,Jabran,Rafique';

$csv->fromString($str);

// Output with headers:
$csv->parse();

Array(
  [id] => 1,
  [first_name] => 'Jabran',
  [last_name] => 'Rafique'
)

// Output without headers:
$csv->parse(false);

Array(
  [0] => array(
    [0] => 'id',
    [1] => 'first_name',
    [2] => 'last_name'
 ),
  [1] => array(
    [0] => 1,
    [1] => 'Jabran',
    [2] => 'Rafique'
 )
)

License

© 2015—2017 MIT License - Jabran Rafique, (*14)

Analytics, (*15)

The Versions

10/02 2017

dev-master

9999999-dev https://github.com/jabranr/csv-parser

Parse CSV data from a file, stream or string

  Sources   Download

MIT License

The Requires

  • php >=5.3.0

 

The Development Requires

csv parser php

10/02 2017

dev-develop

dev-develop https://github.com/jabranr/csv-parser

Parse CSV data from a file, stream or string

  Sources   Download

MIT License

The Requires

  • php >=5.3.0

 

The Development Requires

csv parser php

10/02 2017

2.1.1

2.1.1.0 https://github.com/jabranr/csv-parser

Parse CSV data from a file, stream or string

  Sources   Download

MIT License

The Requires

  • php >=5.3.0

 

The Development Requires

csv parser php

09/02 2017

2.1.0

2.1.0.0 https://github.com/jabranr/csv-parser

Parse CSV data from a file, stream or string

  Sources   Download

MIT License

The Requires

  • php >=5.3.0

 

The Development Requires

csv parser php

07/08 2016

2.0.4

2.0.4.0 https://github.com/jabranr/csv-parser

Parse CSV data from a file, stream or string

  Sources   Download

MIT License

The Requires

  • php >=5.3.0

 

The Development Requires

csv parser php

10/12 2015

2.0.3

2.0.3.0 https://github.com/jabranr/csv-parser

Parse CSV data from a file, stream or string

  Sources   Download

MIT License

The Requires

  • php >=5.3.0

 

The Development Requires

csv parser php

25/10 2015

2.0.2

2.0.2.0 https://github.com/jabranr/csv-parser

Parse CSV data from a file, stream or string

  Sources   Download

MIT License

The Requires

  • php >=5.3.0

 

The Development Requires

csv parser php

07/04 2015

2.0.1

2.0.1.0 https://github.com/jabranr/csv-parser

Parse CSV data from a file, stream or string

  Sources   Download

MIT License

The Requires

  • php >=5.3.0

 

csv parser php

31/03 2015

1.0.1

1.0.1.0 https://github.com/jabranr/csv-parser

Parse CSV data from a file, stream or string

  Sources   Download

MIT License

The Requires

  • php >=5.3.0

 

csv parser php

31/03 2015

1.0.0

1.0.0.0 https://github.com/jabranr/csv-parser

Parse CSV data from a file, stream or string

  Sources   Download

MIT License

The Requires

  • php >=5.3.0

 

csv parser php