承接 kzykhys/php-csv-parser 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

kzykhys/php-csv-parser

最新稳定版本:1.4.1

Composer 安装命令:

composer require kzykhys/php-csv-parser

包简介

Convert CSV to array (Excel style is fully suppoted!)

README 文档

README

Convert CSV to array/Iterator (Excel style is fully suppoted!)

Latest Stable Version Build Status Coverage Status SensioLabsInsight

Why PHPCsvParser?

As you know, PHP has built-in fgetcsv function. But has some probrems:

  • Line breaks in the cell
  • Multibyte string (especially NON UTF-8)
  • Double quote in the cell

Requirements

PHP5.3.3 or later

Installation

Create or modify your composer.json

{
    "require": {
        "kzykhys/php-csv-parser": ">1.4"
    }
}

And run

$ php composer.phar install

Usage

Parse a CSV file

1,"some text",150
2,"some multi line
text",2000
<?php

require('./vendor/autoload.php');

$parser = \KzykHys\CsvParser\CsvParser::fromFile('./test.csv');
$result = $parser->parse();

var_dump($result);

This is the same as:

<?php

require('./vendor/autoload.php');

$iterator = new \SplFileObject('./test.csv');
$parser = new \KzykHys\CsvParser\CsvParser($iterator);
$result = $parser->parse();

var_dump($result);

Parse from string

<?php

require('./vendor/autoload.php');

$parser = \KzykHys\CsvParser\CsvParser::fromString($string);
$result = $parser->parse();

var_dump($result);

Parse from array/Iterator

<?php

require('./vendor/autoload.php');

$parser = \KzykHys\CsvParser\CsvParser::fromArray(array('a,b,c,d', 'e,f,g,h'));
$result = $parser->parse();

$iterator = new ArrayIterator(array('a,b,c,d', 'e,f,g,h'));
$parser2 = new \KzykHys\CsvParser\CsvParser($iterator);
$result2 = $parser2->parse();

var_dump($result);
var_dump($result2);

Handling Large files

The class \KzykHys\CsvParser\CsvParser itself is Traversable. so You can convert CSV lines on-the-fly.

Following example is the best choice for performance:

<?php

require('./vendor/autoload.php');

$parser = \KzykHys\CsvParser\CsvParser::fromFile('./test.csv');

foreach ($parser as $record) {
    // handles each record
    var_dump($record);
}

Options

You can pass the options to 2nd argument of each static methods.

  • CsvParser::fromFile($file, $options);
  • CsvParser::fromString($string, $options);
  • CsvParser::fromArray($array, $options);
  • new CsvParser($iterator, $options);

Available options are:

Option Type Description Default
delimiter string The field delimiter (one character only) ,
enclosure string The field enclosure character (one character only) "
encoding string The type of encoding CP932
offset integer (>=0) The sequence will start at that offset 0
limit integer (>=-1) Limit maximum count of records -1 (unlimited)
header array or false Use the specified index instead of the column number false

Testing

Just run phpunit (PHPUnit is required)

Author

Kazuyuki Hayashi (@kzykhys)

Changelog

see CHANGELOG

License

The MIT License

统计信息

  • 总下载量: 255.53k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 57
  • 点击次数: 1
  • 依赖项目数: 4
  • 推荐数: 1

GitHub 信息

  • Stars: 54
  • Watchers: 5
  • Forks: 24
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2013-01-31