PHP Classes

File: README.md

Recommend this page to a friend!
  Classes of Jabran Rafique   CSV Parser   README.md   Download  
File: README.md
Role: Documentation
Content type: text/markdown
Description: Auxiliary data
Class: CSV Parser
Parse CSV data to indexed or associative arrays
Author: By
Last change:
Date: 9 years ago
Size: 1,545 bytes
 

Contents

Class file image Download

CSV Parser (PHP)

PHP client to parse CSV data from a file, stream or string into indexed or associative arrays.

Install

Install using composer

#composer.json

{
  "require": {
    "jabranr/csv-parser": ">=1.0.0"
  }
}

Run following to install

$ comsposer install

Use

Initiate a new instance

$csv = new CSV_Parser();

API

Get data from a string

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

Get data from a stream

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

Get data from a file

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

Parse data for output

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

Example

Example input string

$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 MIT License - Jabran Rafique

Analytics