2017 © Pedro Peláez
 

library phpcsvparser

A simple csv parser for php.

image

jralph/phpcsvparser

A simple csv parser for php.

  • Monday, February 15, 2016
  • by jralph
  • Repository
  • 2 Watchers
  • 2 Stars
  • 20 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 9 Versions
  • 0 % Grown

The README.md

PHP CSV Parser Build Status SensioLabsInsight

Latest Stable Version Total Downloads Latest Unstable Version License, (*1)

PHP CSV Parser is a simple csv parser written in php. It can parse csv strings or files., (*2)

It utilises Laravel's Support Library to help with providing a simple and clean structure., (*3)

Contents

Installation

PHP CSV Parser is available as a composer package and can be added to your composer.json like so., (*4)

"require": {
    ...
    "jralph/phpcsvparser": "2.*"
    ...
}

Usage

Using the CSV parser is simple and can be done by directly accessing the Jralph\PHPCSVParser\ParserManager class, or either of the Parser classes (Jralph\PHPCSVParser\Parsers\StringParser or Jralph\PHPCSVParser\Parsers\FileParser)., (*5)

For ease of use, there is also a facade that has been created. Jralph\PHPCSVParser\Facades\Parser. This facade can call the create method on the ParserManager object., (*6)

create('csv string of path to file here.');

// Using the File Parser (NO AUTO DETECTION)
$parser = new FileParser('path/to/file/here');

// Using the String Parser (NO AUTO DETECTION)
$parser = new StringParser('"csv","string","here"');

// Once you have an instance of a parser, you can parse the csv.
// You can optionally pass in any of the paramaters below.
$csv = $parser->parse($delimiter = ',', $enclosure = '"', $escape = '\\');

// Or no paramaters at all.
$csv = $parser->parse();

// Maybe your csv does not have column headings.
$csv = $parser->withoutHeadings()->parse();

?>

Working with the CSV

Once you have parsed the CSV, you will be given a CSV object. This object contains a collection of headers (if any) and a collection of all of the csv rows., (*7)

parse();

foreach ($csv->rows() as $row) {
    // Do something with the rows.
}

?>

CSV Rows

When looping through csv rows, each row is returned as an instance of the CSVRow object., (*8)

You can access data through this object in many ways., (*9)

parse();

foreach ($csv->rows() as $row) {
    // We have not told the parser to process without headings, so we can access
    // each row by its heading name.
    echo $row->heading1; // As an object.
    echo $row['heading2']; // As an array.
    echo $row->getRow('heading3'); // By a method.
}

// If we do not have or know the headings, we can loop through the row attributes.
foreach ($csv->rows() as $row) {
    // Loop through the $row object.
    foreach ($row as $column) {
        echo $column;
    }

    // Get an array of all attributes.
    $attributes = $row->getAttributes();
}

?>

Arrays and JSON

The following objects contain convertors to convert the object to an array and to json., (*10)

  • CSV
  • CSVRow
parse();

echo $csv->toJson(); // Echo the entire csv, headings and rows, as json.

foreach ($csv->rows() as $row) {
    echo $row->toJson(); // Echo the entire row as json.
}

?>

The Versions

15/02 2016

dev-master

9999999-dev

A simple csv parser for php.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Joseph Ralph

04/02 2015

3.0.1

3.0.1.0

A simple csv parser for php.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Joseph Ralph

04/02 2015

v3.0.0

3.0.0.0

A simple csv parser for php.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Joseph Ralph

27/11 2014

2.0.2

2.0.2.0

A simple csv parser for php.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Joseph Ralph

27/08 2014

2.0.1

2.0.1.0

A simple csv parser for php.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Joseph Ralph

27/08 2014

2.0.0

2.0.0.0

A simple csv parser for php.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Joseph Ralph

17/07 2014

1.0.2

1.0.2.0

A simple csv parser for php.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Joseph Ralph

17/07 2014

1.0.1

1.0.1.0

A simple csv parser for php.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Joseph Ralph

16/07 2014

1.0.0

1.0.0.0

A simple csv parser for php.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Joseph Ralph