承接 tonyfarney/csv-reader 相关项目开发

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

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

tonyfarney/csv-reader

Composer 安装命令:

composer require tonyfarney/csv-reader

包简介

Ease to use CSV Reader for PHP 7+

关键字:

README 文档

README

Ease to use CSV Reader for PHP 7+

Instalation

In your project root, run the following composer command:

$ composer require tonyfarney/csv-reader

General orientations

CSVReader loads CSV from string and returns an array containing all lines read. Each position is a line e each line is an array of columns. For all the examples of use, we will consider the following code as the base implementation:

<?php
require __DIR__.'/vendor/autoload.php';
    
use \CSVReader\CSVReader;
    
$csvWithHeader = <<<CSV
name,role,age
"Tony Farney","Developer",30
"The Coffe Guy","Intern",18
"Someone Else","Developer",26
CSV;
    
$csvWithoutHeader = <<<CSV
"Tony Farney","Developer",30
"The Coffe Guy","Intern",18
"Someone Else","Developer",26
CSV;
    
$reader = new CSVReader();

Loading CSV with a header line

// Reads all the three columns
$lines = $reader->setHeader(['name', 'role', 'age'])->load($csvWithHeader);

// The colmuns order in the header definition doesn't matter
$lines = $reader->setHeader(['role', 'name', 'age'])->load($csvWithHeader); // Works exactly like the previous line

// Columns in CSV that are not in definition causes error
$lines = $reader->setHeader(['role', 'name'])->load($csvWithHeader); // CSVReader\CSVReaderException: The following columns are not expected in the header: age

// Columns in definition that are not in CSV doesn't causes error. That allows the "optional columns" behavior
$lines = $reader->setHeader(['role', 'name', 'age', 'another_column'])->load($csvWithHeader); // Works perfectly

All the succesfull reads above will output something like:

array(3) {
  [0]=>
  array(3) {
    ["name"]=>
    string(11) "Tony Farney"
    ["role"]=>
    string(9) "Developer"
    ["age"]=>
    string(2) "30"
  }
  [1]=>
  array(3) {
    ["name"]=>
    string(13) "The Coffe Guy"
    ["role"]=>
    string(6) "Intern"
    ["age"]=>
    string(2) "18"
  }
  [2]=>
  array(3) {
    ["name"]=>
    string(12) "Someone Else"
    ["role"]=>
    string(9) "Developer"
    ["age"]=>
    string(2) "26"
  }
}

Loading CSV without a header line

// Sets the indexes for the columns array, outputing exact the same as the header examples
$lines = $reader->setIndexing(['name', 'role', 'age'])->load($csvWithoutHeader);

The number of indexes definition must match with the read columns from CSV

$lines = $reader->setIndexing(['name', 'role', 'age', 'another_column'])->load($csvWithoutHeader);
$lines = $reader->setIndexing(['name', 'role')->load($csvWithoutHeader);

Both lines above will throw the following error:

CSVReader\CSVReaderException: The following lines of the CSV has mismatching number of values: 2, 3, 4

It's possible read CSV without have an indexed array of columns:

$lines = $reader->load($csvWithoutHeader);

Output:

array(3) {
  [0]=>
  array(3) {
    [0]=>
    string(11) "Tony Farney"
    [1]=>
    string(9) "Developer"
    [2]=>
    string(2) "30"
  }
  [1]=>
  array(3) {
    [0]=>
    string(13) "The Coffe Guy"
    [1]=>
    string(6) "Intern"
    [2]=>
    string(2) "18"
  }
  [2]=>
  array(3) {
    [0]=>
    string(12) "Someone Else"
    [1]=>
    string(9) "Developer"
    [2]=>
    string(2) "26"
  }
}

Tips

When reusing the same reader instance for read CSV with different configurations, it's necessary do a reset to avoid errors:

// First load
$lines = $reader->setHeader(['name', 'role', 'age'])->load($csvWithHeader);
// Second load requires a reset
$lines = $reader->reset()->load($csvWithoutHeader);

The reader will try to detect automatically the column delimiter by reading the first line and determining the usual delimiter (",", ";", "|", "\t") with most occurences, but it's possible manually set what delimiter to use:

$reader->setColumnDelimiter(';');

It's possible to set the enclosure (defaults to ") and the escape (defaults to ) characters :

$reader->setEnclosure('"')->setScape('\\');

Contributions/Support

You are welcome to contribute with improvement, bug fixes, new ideas, etc. Any doubt/problem, please contact me by email: tonyfarney@gmail.com. I'll be glad to help you ;)

tonyfarney/csv-reader 适用场景与选型建议

tonyfarney/csv-reader 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 145 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 05 月 29 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「csv」 「reader」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 tonyfarney/csv-reader 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 tonyfarney/csv-reader 我们能提供哪些服务?
定制开发 / 二次开发

基于 tonyfarney/csv-reader 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 145
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 11
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-05-29