2017 © Pedro Peláez
 

library csvhelper

Helper class for working with CSV files

image

tpmanc/csvhelper

Helper class for working with CSV files

  • Wednesday, November 30, 2016
  • by tpmanc
  • Repository
  • 1 Watchers
  • 1 Stars
  • 107 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 13 % Grown

The README.md

CSV Helper

Helper class for working with CSV files., (*1)

Install via Composer

Run the following command, (*2)

$ composer require tpmanc/csvhelper "*"

or add, (*3)

$ "tpmanc/csvhelper": "*"

to the require section of your composer.json file., (*4)

File reading

file.csv:, (*5)

Russia;Moscow;
France;Paris;
Great Gritain;London;
use tpmanc\csvhelper\CsvHelper;

...

CsvHelper::open('files/file.csv')->parse(function($line) {
    echo $line[0] . ': ' . $line[1];
});

Result:, (*6)

Russia: Moscow
France: Paris
Great Gritain: London

Custom delimiter

file.csv:, (*7)

Russia|Moscow|
France|Paris|
Great Gritain|London|
use tpmanc\csvhelper\CsvHelper;

...

CsvHelper::open('files/file.csv')->delimiter('|')->parse(function($line) {
    echo $line[0] . ': ' . $line[1];
});

Result:, (*8)

Russia: Moscow
France: Paris
Great Gritain: London

Change encoding

use tpmanc\csvhelper\CsvHelper;

...

CsvHelper::open('files/file.csv')->encode('cp1251', 'utf-8')->parse(function($line) {
    echo $line[0] . ': ' . $line[1];
});

Offset and limit

file.csv:, (*9)

Russia;Moscow;
France;Paris;
Great Gritain;London;
use tpmanc\csvhelper\CsvHelper;

...

CsvHelper::open('files/file.csv')->offset(1)->limit(1)->parse(function($line) {
    echo $line[0] . ': ' . $line[1];
});

Result:, (*10)

France: Paris

Using variables from the parent scope

file.csv:, (*11)

Russia;Moscow;
France;Paris;
Great Gritain;London;
use tpmanc\csvhelper\CsvHelper;

...

$lineCount = 0;
$array = [];
CsvHelper::open('files/file.csv')->parse(function($line) use(&$lineCount, &$array) {
    $lineCount++;
    $array[] = $line[0];
});
echo $lineCount;
echo $array[0];
echo $array[1];

Result:, (*12)

3
Russia
France

Create new file

use tpmanc\csvhelper\CsvHelper;

...

$file = CsvHelper::create()->delimiter(';');

$file->encode('cp1251', 'utf-8'); // change encoding
$file->addLine('1.;France;'); // add row to file by string
$file->addLine([
    2,
    'Germany'
]); // add row to file by array

$file->save('./new-file.csv');

The Versions

30/11 2016

dev-master

9999999-dev https://github.com/tpmanc/csv-helper

Helper class for working with CSV files

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

The Development Requires

csv helper reading

17/08 2016

1.1.1

1.1.1.0 https://github.com/tpmanc/csv-helper

Helper class for working with CSV files

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

The Development Requires

csv helper reading

30/12 2015

1.1

1.1.0.0 https://github.com/tpmanc/csv-helper

Helper class for working with CSV files

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

csv helper reading

07/10 2015

1.0

1.0.0.0 https://github.com/tpmanc/csv-helper

Helper class for working with CSV files

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4.0

 

csv helper reading