jralph/phpcsvparser
Composer 安装命令:
composer require jralph/phpcsvparser
包简介
A simple csv parser for php.
README 文档
README
PHP CSV Parser is a simple csv parser written in php. It can parse csv strings or files.
It utilises Laravel's Support Library to help with providing a simple and clean structure.
Contents
Installation
PHP CSV Parser is available as a composer package and can be added to your composer.json like so.
"require": {
...
"jralph/phpcsvparser": "2.*"
...
}
Usage
Using the CSV parser is simple and can be done by directly accessing the Jralph\PHPCSVParser\ParserManager class, or either of the Parser classes (Jralph\PHPCSVParser\Parsers\StringParser or Jralph\PHPCSVParser\Parsers\FileParser).
For ease of use, there is also a facade that has been created. Jralph\PHPCSVParser\Facades\Parser. This facade can call the create method on the ParserManager object.
<?php
use Jralph\PHPCSVParser\Facades\Parser;
use Jralph\PHPCSVParser\ParserManager;
use Jralph\PHPCSVParser\Parsers\FileParser;
use Jralph\PHPCSVParser\Parsers\StringParser;
// Using the Facade (AUTO DETECTS STRING OR FILE)
$parser = Parser::create('csv string or path to file here.');
// Using the ParserManager (AUTO DETECTS STRING OR FILE)
$parser = new ParserManager;
$parser->create('csv string of path to file here.');
// Using the File Parser (NO AUTO DETECTION)
$parser = new FileParser('path/to/file/here');
// Using the String Parser (NO AUTO DETECTION)
$parser = new StringParser('"csv","string","here"');
// Once you have an instance of a parser, you can parse the csv.
// You can optionally pass in any of the paramaters below.
$csv = $parser->parse($delimiter = ',', $enclosure = '"', $escape = '\\');
// Or no paramaters at all.
$csv = $parser->parse();
// Maybe your csv does not have column headings.
$csv = $parser->withoutHeadings()->parse();
?>
Working with the CSV
Once you have parsed the CSV, you will be given a CSV object. This object contains a collection of headers (if any) and a collection of all of the csv rows.
<?php
use Jralph\PHPCSVParser\Facades\Parser;
use Jralph\PHPCSVParser\ParserManager;
use Jralph\PHPCSVParser\Parsers\FileParser;
use Jralph\PHPCSVParser\Parsers\StringParser;
$parser = Parser::create('csv string or path to file here.');
$csv = $parser->parse();
foreach ($csv->rows() as $row) {
// Do something with the rows.
}
?>
CSV Rows
When looping through csv rows, each row is returned as an instance of the CSVRow object.
You can access data through this object in many ways.
<?php
use Jralph\PHPCSVParser\Facades\Parser;
use Jralph\PHPCSVParser\ParserManager;
use Jralph\PHPCSVParser\Parsers\FileParser;
use Jralph\PHPCSVParser\Parsers\StringParser;
$parser = Parser::create('csv string or path to file here.');
$csv = $parser->parse();
foreach ($csv->rows() as $row) {
// We have not told the parser to process without headings, so we can access
// each row by its heading name.
echo $row->heading1; // As an object.
echo $row['heading2']; // As an array.
echo $row->getRow('heading3'); // By a method.
}
// If we do not have or know the headings, we can loop through the row attributes.
foreach ($csv->rows() as $row) {
// Loop through the $row object.
foreach ($row as $column) {
echo $column;
}
// Get an array of all attributes.
$attributes = $row->getAttributes();
}
?>
Arrays and JSON
The following objects contain convertors to convert the object to an array and to json.
CSVCSVRow
<?php
use Jralph\PHPCSVParser\Facades\Parser;
use Jralph\PHPCSVParser\ParserManager;
use Jralph\PHPCSVParser\Parsers\FileParser;
use Jralph\PHPCSVParser\Parsers\StringParser;
$parser = Parser::create('csv string or path to file here.');
$csv = $parser->parse();
echo $csv->toJson(); // Echo the entire csv, headings and rows, as json.
foreach ($csv->rows() as $row) {
echo $row->toJson(); // Echo the entire row as json.
}
?>
jralph/phpcsvparser 适用场景与选型建议
jralph/phpcsvparser 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 36 次下载、GitHub Stars 达 1, 最近一次更新时间为 2014 年 07 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 jralph/phpcsvparser 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jralph/phpcsvparser 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 36
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2014-07-16