Skip to content
/ csv Public
forked from goodby/csv

Goodby CSV is a high memory efficient flexible and extendable open-source CSV import/export library for PHP 5.3. 1. Memory Management Free This library designed for memory unbreakable. It will not be accumulated in the memory whole rows. The importer read CSV file and execute callback function line by line. 2. Multibyte support This library supp…

License

Notifications You must be signed in to change notification settings

myusuf/csv

Repository files navigation

Goodby, CSV

Build Status

What is "Goodby CSV"?

Goodby CSV is a flexible and extendable open-source CSV import/export library.

// Sample code... writing...

Features

1. Memory Management Free

This library designed for memory unbreakable. It will not be accumulated in the memory whole rows. The importer read CSV file and execute callback function line by line.

2. Multibyte support

This library supports mulitbyte input/output: for example, SJIS-win, EUC-JP and UTF-8.

3. Ready to Use for Enterprise Applications

Goodby CSV is fully unit-tested. The library is stable and ready to be used in large projects like enterprise applications.

Requirements

  • PHP 5.3.2 or later
  • mbstring

Installation

Install composer in your project:

curl -s http://getcomposer.org/installer | php

Create a composer.json file in your project root:

{
    "require": {
        "goodby/csv": "*"
    }
}

Install via composer:

php composer.phar install

License

Csv is open-sourced software licensed under the MIT License - see the LICENSE file for details

Documentation

Import to Database via PDO

<?php

use Goodby\CSV\Import\Standard\Lexer;
use Goodby\CSV\Import\Standard\Interpreter;

$pdo = new PDO('mysql:host=localhost;dbname=test', 'root', 'root');
$pdo->query('CREATE TABLE IF NOT EXISTS user (id INT, `name` VARCHAR(255), email VARCHAR(255))');

$config = new LexerConfig();
$lexer = new Lexer($config);

$interpreter = new Interpreter();

$interpreter->addObserver(function(array $columns) use ($pdo) {
    $stmt = $pdo->prepare('INSERT INTO user (id, name, email) VALUES (?, ?, ?)');
    $stmt->execute($columns);
});

$lexer->parse('user.csv', $interpreter);

Export from array

<?php

use Goodby\CSV\Export\Standard\Exporter;
use Goodby\CSV\Export\Standard\ExporterConfig;

$config = new ExporterConfig();
$exporter = new Exporter($config);

$exporter->export('php://output', array(
    array('1', 'alice', '[email protected]'),
    array('2', 'bob', '[email protected]'),
    array('3', 'carol', '[email protected]'),
));

Export from database via PDO

<?php

use Goodby\CSV\Export\Standard\Exporter;
use Goodby\CSV\Export\Standard\ExporterConfig;

use Goodby\CSV\Export\Standard\Collection\PdoCollection;

$pdo = new PDO('mysql:host=localhost;dbname=test', 'root', 'root');

$pdo->query('CREATE TABLE IF NOT EXISTS user (id INT, `name` VARCHAR(255), email VARCHAR(255))');
$pdo->query("INSERT INTO user VALUES(1, 'alice', '[email protected]')");
$pdo->query("INSERT INTO user VALUES(2, 'bob', '[email protected]')");
$pdo->query("INSERT INTO user VALUES(3, 'carol', '[email protected]')");

$config = new ExporterConfig();
$exporter = new Exporter($config);

$stmt = $pdo->prepare("SELECT * FROM user");
$stmt->execute();

$exporter->export('php://output', new PdoCollection($stmt));

Contributing

We works under test driven development.

Checkout master source code from github:

hub clone goodby/csv

Install components via composer:

# If you don't have composer.phar
./scripts/bundle-devtools.sh .

# If you have composer.phar
composer.phar install --dev

Run phpunit:

./vendor/bin/phpunit

Acknowledgement

editing...

About

Goodby CSV is a high memory efficient flexible and extendable open-source CSV import/export library for PHP 5.3. 1. Memory Management Free This library designed for memory unbreakable. It will not be accumulated in the memory whole rows. The importer read CSV file and execute callback function line by line. 2. Multibyte support This library supp…

Resources

License

Stars

Watchers

Forks

Packages

No packages published