2017 © Pedro Peláez
 

library csv-parser

CSV Parser for SplFileObject

image

volcanus/csv-parser

CSV Parser for SplFileObject

  • Thursday, September 28, 2017
  • by k-holy
  • Repository
  • 1 Watchers
  • 1 Stars
  • 1,628 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 8 Versions
  • 13 % Grown

The README.md

Volcanus_CsvParser

Latest Stable Version Continuous Integration, (*1)

CSV文字列の解析を行うためのPHPクラスライブラリです。, (*2)

Standard PHP Library (SPL) のファイル入出力用クラス SplFileObject と組み合わせることで、簡単にファイルからの読み込みを行えます。, (*3)

Volcanus_Csv からの派生物で、CSV入力に関する最低限の機能を移植しています。, (*4)

環境

  • PHP 8.1以降
  • mbstring拡張

使い方

<?php

$parser = new \Volcanus\CsvParser\CsvParser([
    'delimiter'      => ',',
    'enclosure'      => '"',
    'escape'         => '"',
    'inputEncoding'  => 'SJIS',
    'outputEncoding' => 'UTF-8',
    'sanitizing'     => true,
]);

$csvFile = new \SplFileObject('php://temp', '+r');
$csvFile->fwrite(mb_convert_encoding("1,田中\r\n", 'SJIS', 'UTF-8'));
$csvFile->fwrite(mb_convert_encoding("2,山田\r\n", 'SJIS', 'UTF-8'));
$csvFile->fwrite(mb_convert_encoding("3,鈴木\r\n", 'SJIS', 'UTF-8'));
$csvFile->rewind();

$users = [];

foreach ($csvFile as $line) {

    // 1件分のレコード取得が終了するまで各行をパース
    if (!$parser->parse($line)) {
        continue;
    }

    $csv = $parser->getBuffer();

    $row = $parser->convert($csv);

    // 空行にはNULLが返されるので無視
    if (is_null($row)) {
        continue;
    }

    // CSVのフィールドをオブジェクトに取得
    $user = new \stdClass();
    $user->id   = $row[0];
    $user->name = $row[1];

    $users[] = $user;
}

echo $users[0]->id; // 1
echo $users[0]->name; // 田中
echo $users[1]->id; // 2
echo $users[1]->name; // 山田
echo $users[2]->id; // 3
echo $users[2]->name; // 鈴木

注意点

SplFileObjectを前提としていますが、CSVの加工は独自の処理を行なっています。 そのため、SplFileObject::setCsvControl() で設定した値は利用されません。 また、fgetcsv() , SplFileObject::fgetcsv() , str_getcsv() といったPHP標準の関数とは異なる結果を返す可能性があります。, (*5)

復帰・改行・水平タブ・スペース以外の制御コードを自動で削除するサニタイジング機能を備えていますが、初期設定では無効になっています。, (*6)

設定値の取得にはプロパティアクセス、配列アクセスを利用できますが、設定値のセットには利用できません。 コンストラクタのパラメータで指定するか、config()メソッドを呼ぶ必要があります。, (*7)

The Versions

28/09 2017

dev-master

9999999-dev https://github.com/k-holy/volcanus-csv-parser

CSV Parser for SplFileObject

  Sources   Download

MIT

The Requires

  • ext-mbstring *
  • php ^5.6||^7.0

 

The Development Requires

by Avatar k-holy

csv

28/09 2017

dev-develop

dev-develop https://github.com/k-holy/volcanus-csv-parser

CSV Parser for SplFileObject

  Sources   Download

MIT

The Requires

  • ext-mbstring *
  • php ^5.6||^7.0

 

The Development Requires

by Avatar k-holy

csv

28/09 2017

2.0.0

2.0.0.0 https://github.com/k-holy/volcanus-csv-parser

CSV Parser for SplFileObject

  Sources   Download

MIT

The Requires

  • php ^5.6||^7.0
  • ext-mbstring *

 

The Development Requires

by Avatar k-holy

csv

28/09 2017

1.2.0

1.2.0.0 https://github.com/k-holy/volcanus-csv-parser

CSV Parser for SplFileObject

  Sources   Download

MIT

The Requires

  • php >=5.3.9
  • ext-mbstring *

 

The Development Requires

by Avatar k-holy

csv

28/09 2017

1.2.x-dev

1.2.9999999.9999999-dev https://github.com/k-holy/volcanus-csv-parser

CSV Parser for SplFileObject

  Sources   Download

MIT

The Requires

  • php >=5.3.9
  • ext-mbstring *

 

The Development Requires

by Avatar k-holy

csv

17/04 2015

1.1.3

1.1.3.0 https://github.com/k-holy/volcanus-csv-parser

CSV Parser for SplFileObject

  Sources   Download

MIT

The Requires

  • php >=5.3
  • ext-mbstring *

 

by Avatar k-holy

csv

25/11 2013

1.1.2

1.1.2.0 https://github.com/k-holy/volcanus-csv-parser

CSV Parser for SplFileObject

  Sources   Download

MIT

The Requires

  • php >=5.3
  • ext-mbstring *

 

The Development Requires

by Avatar k-holy

csv

21/10 2013

1.1.1

1.1.1.0 https://github.com/k-holy/volcanus-csv-parser

CSV Parser for SplFileObject

  Sources   Download

MIT

The Requires

  • php >=5.3
  • ext-mbstring *

 

by Avatar k-holy

csv