yidas/csv
Composer 安装命令:
composer require yidas/csv
包简介
PHP CSV writer & reader with settings of quoting enclosure and encoding
README 文档
README
PHP CSV
PHP CSV writer & reader with settings of quoting enclosure and encoding
Features
-
Support generating Double Quotes enclosure for all entities setting
-
Support Encoding setting for local dialect
-
Elegent Interface for setup and use
OUTLINE
DEMONSTRATION
Quickstart with specifying the file name directly:
// Read the CSV file $csvReader = new yidas\csv\Reader('/tmp/file.csv'); $rows = $csvReader->readRows(); $csvReader->fclose(); // Write the CSV file $csvWriter = new yidas\csv\Writer('/tmp/file.csv'); $csvWriter->writeRows($rows); $csvWriter->fclose();
Write to CSV
Open a file and use libray to write in CSV format:
$fp = fopen("/tmp/file.csv", 'w'); $csvWriter = new yidas\csv\Writer($fp, [ // 'quoteAll' => true, // 'encoding' => 'UTF-8' ]); $csvWriter->writeRow(["First", 'Second']); $csvWriter->writeRows([ ["Normal", 'Double"Quote'], ["It's a context,\nNew line.", 'Encoded中文'], ]); fclose($fp);
The content of generated CSV file will be:
"First","Second" "Normal","Double""Quote" "It's a context, New line.","Encoded中文"
In default setting, it will always add double quotes with
UTF-8encoding.
Read from CSV
Open a CSV file with local encoding (Big5) and use libray to read with UTF-8 encoding:
$fp = fopen("/tmp/file.csv", 'r'); $csvReader = new yidas\csv\Reader($fp, [ 'encoding' => 'Big5' ]); $firstRow = $csvReader->readRow(); $remainingRows = $csvReader->readRows(); fclose($fp);
REQUIREMENTS
This library requires the following:
- PHP CLI 5.4.0+
INSTALLATION
Run Composer in your project:
composer require yidas/csv
Then you could use the class after Composer is loaded on your PHP project:
require __DIR__ . '/vendor/autoload.php'; use yidas\csv\Writer; use yidas\csv\Reader;
USAGE
Options
The options in parameter 2 for Writer/Reader class are as below:
quoteAll
Controls when quotes should be always generated by the writer.
Encoding
Controls which the encoding should be generated by the writer/reader.
By defaule, Microsoft Excel will open CSV file with local encoding. For example: Excel in Chinese(Traditional) Windows will open CSV with Big5 encoding.
Writer
$csvWriter = new yidas\csv\Writer($fp, [ // 'quoteAll' => true, // 'encoding' => 'UTF-8' ]);
writeRow()
Write the row parameter to the writer’s file stream, formatted according to the current setting.
public static array writeRow(array $rowData)
writeRows()
Write the rows parameter to the writer’s file stream, formatted according to the current setting.
public static array writeRows(array $rowsData)
Reader
$csvReader = new yidas\csv\Reader($fp, [ // 'encoding' => 'UTF-8' ]);
readRow()
Read a row from current file pointer.
public static array readRow()
Example:
while ( ($row = $csvReader->readRow($file) ) !== FALSE ) { var_dump($row); }
readRows()
Read all the rows from current file pointer.
public static array readRows()
Example:
$rows = $csvReader->readRows(); var_dump($rows);
Exceptions
try { $csvWriter = new yidas\csv\Writer($fp, [ // 'quoteAll' => true, // 'encoding' => 'UTF-8' ]); $csvWriter->writeRow(["First", 'Second']); } catch (\Exception $e) { echo 'Code:' . $e->getCode() . ' Message:' . $e->getMessage(); }
REFERENCES
yidas/csv 适用场景与选型建议
yidas/csv 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.62k 次下载、GitHub Stars 达 6, 最近一次更新时间为 2021 年 09 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「csv」 「csv writer」 「csv reader」 「Excel CSV」 「Quoting CSV」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 yidas/csv 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 yidas/csv 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 yidas/csv 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Simple RSS generator library for PHP 5.5 or later. clone from Bhaktaraz Bhatta ttps://github.com/bhaktaraz/php-rss-generator
laravel facade to read/write csv file
Collection of tools to use the full power of the Enyalius framework
Alfabank REST API integration
CSS reader and writer with full CSS3 support, already supporting huge parts of the current CSS4 spec. It supports media queries, comments, value optimization and more... It offers full Unicode support and can handle also large CSS sources. Requires PHP 5.4+.
Fast Excel import/export for Laravel
统计信息
- 总下载量: 1.62k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 10
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-09-02