jralph / phpcsvparser
PHP 简单的 CSV 解析器。
3.0.1
2015-02-04 16:38 UTC
Requires
- php: >=5.4.0
- illuminate/contracts: 5.*
- illuminate/support: 5.*
Requires (Dev)
- phpunit/phpunit: 4.*
README
PHP CSV Parser 是用 PHP 编写的简单 CSV 解析器。它可以解析 CSV 字符串或文件。
它利用 Laravel 的支持库 来帮助提供简单和清晰的架构。
内容
安装
PHP CSV Parser 以 composer 包的形式提供,可以像这样添加到您的 composer.json 中。
"require": {
...
"jralph/phpcsvparser": "2.*"
...
}
使用
使用 CSV 解析器很简单,可以直接访问 Jralph\PHPCSVParser\ParserManager
类,或者任何解析器类(Jralph\PHPCSVParser\Parsers\StringParser
或 Jralph\PHPCSVParser\Parsers\FileParser
)。
为了方便使用,还创建了一个 facade
,即 Jralph\PHPCSVParser\Facades\Parser
。这个 facade
可以调用 ParserManager
对象的 create
方法。
<?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();
?>
处理 CSV
解析完 CSV 后,您将得到一个 CSV
对象。该对象包含一组(如果有的话)头和一个包含所有 CSV 行的集合。
<?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 行
在遍历 CSV 行时,每一行都作为 CSVRow 对象的实例返回。
您可以通过多种方式通过此对象访问数据。
<?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();
}
?>
数组和 JSON
以下对象包含转换器,可以将对象转换为数组并转换为 JSON。
CSV
CSVRow
<?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.
}
?>