Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jabranr/csv-parser
PHP client to parse CSV data from a path, file, stream, resource or string into indexed or associative arrays.
https://github.com/jabranr/csv-parser
csv parser php
Last synced: about 2 months ago
JSON representation
PHP client to parse CSV data from a path, file, stream, resource or string into indexed or associative arrays.
- Host: GitHub
- URL: https://github.com/jabranr/csv-parser
- Owner: jabranr
- Created: 2015-03-30T21:42:38.000Z (almost 10 years ago)
- Default Branch: develop
- Last Pushed: 2019-03-04T11:49:00.000Z (almost 6 years ago)
- Last Synced: 2024-09-18T04:16:48.779Z (4 months ago)
- Topics: csv, parser, php
- Language: PHP
- Homepage:
- Size: 41 KB
- Stars: 9
- Watchers: 3
- Forks: 6
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# CSV Parser (PHP) [![Build Status](https://travis-ci.org/jabranr/csv-parser.svg?branch=master)](https://travis-ci.org/jabranr/csv-parser) [![Latest Stable Version](https://poser.pugx.org/jabranr/csv-parser/v/stable.svg)](https://packagist.org/packages/jabranr/csv-parser) [![Total Downloads](https://poser.pugx.org/jabranr/csv-parser/downloads.svg)](https://packagist.org/packages/jabranr/csv-parser)
PHP client to parse CSV data from a path, file, stream, resource or string into indexed or associative arrays.
#### Migration from v2 to v3
PHP support updated to 7+
# Install
Install using [composer](http://getcomposer.org)```json
#composer.json{
"require": {
"jabranr/csv-parser": "^3.0"
}
}
```Run following to install
```shell
$ comsposer install
```# Use
Initiate a new instance
```php
$csv = new Jabran\CSV_Parser();
```# Unit tests
If you have `composer` installed globally then:
Run unit tests
```shell
$ cd path/to/csv-parser
$ composer run tests
```If you have `phpunit` installed globally then:
```shell
$ cd path/to/csv-parser
$ phpunit
```# API
Get data from a string
```php
/* @param: string $str */
$csv->fromString($str);
```Get data from a resource (Since v2.0.2)
```php
/* @param: resource $resource (f.e. resource created using fopen()) */
$csv->fromResource($resource);
```Get data from a path/URL (Since v2.0.2)
```php
/* @param: string $path */
$csv->fromPath($path);
```Parse data for output
```php
/**
* 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)
```php
/**
* 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
```php
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 onwardsMIT License - [Jabran Rafique](http://jabran.me)
[![Analytics](https://ga-beacon.appspot.com/UA-50688851-1/csv-parser)](https://github.com/igrigorik/ga-beacon)