jupitern/file-parser
Composer 安装命令:
composer require jupitern/file-parser
包简介
read, filter, parse and format {csv, tsv, dsv, variable-length-delimited} and other txt files
README 文档
README
PHP File Parser.
read, filter, parse and format {csv, tsv, dsv, variable-length-delimited} from files or strings
Requirements
PHP 8.0 or higher.
Installation
Include jupitern/file-parser in your project, by adding it to your composer.json file.
composer require jupitern/file-parser
Usage
Lets parse a csv from a string with contents (animal, category, count): animal,type,count crocodile,reptile,4 dolphin,mammal,0 duck,bird,2 koala,mammal,4 lion,mammal,5 lets parse the file with: - ignore the first line - convert encoding from ISO-9959-1 to UTF-8 - convert lines to objects - remove animals with count 0 - format the animal type to uppercase - group by type $objectsArr = \Jupitern\Parser\FileParser::instance() ->fromString('animal,type,count crocodile,reptile,4 dolphin,mammal,0 duck,bird,2 koala,mammal,4 lion,mammal,5', ',') ->setEncoding('ISO-8859-1', 'UTF-8') ->toObject(['animal', 'type', 'animalCount']) ->filter(function ($line, $lineNumber) { return $lineNumber > 1 && $line->animalCount > 0; }) ->format('type', function ($val) { return strtoupper($val); }) ->group(function ($line) { return $line->type; }) ->parse(); echo '<pre>'; print_r($objectsArr); /* output: Array ( [REPTILE] => Array ( [0] => stdClass Object ( [animal] => crocodile [type] => REPTILE [animalCount] => 4 ) ) [BIRD] => Array ( [0] => stdClass Object ( [animal] => duck [type] => BIRD [animalCount] => 2 ) ) [MAMMAL] => Array ( [0] => stdClass Object ( [animal] => koala [type] => MAMMAL [animalCount] => 4 ) [1] => stdClass Object ( [animal] => lion [type] => MAMMAL [animalCount] => 5 ) ) ) */ Given a csv file "filename.csv" with contents (animal, category, count): animal,type,count crocodile,reptile,4 dolphin,mammal,0 duck,bird,2 koala,mammal,4 lion,mammal,5 lets parse the file with: - ignore the first line - convert encoding from ISO-9959-1 to UTF-8 - convert lines to objects - remove animals with count 0 - format the animal type to uppercase - group by type // read a file to array $objectsArr = \Jupitern\Parser\FileParser::instance() ->fromFile('D:\\aaa.txt', ',') ->setEncoding('ISO-8859-1', 'UTF-8') ->toObject(['animal', 'type', 'animalCount']) ->filter(function ($line, $lineNumber) { return $lineNumber > 1 && $line->animalCount > 0; }) ->format('type', function ($val) { return strtoupper($val); }) ->group(function ($line) { return $line->type; }) ->parse(); echo '<pre>'; print_r($objectsArr); /* output: Array ( [REPTILE] => Array ( [0] => stdClass Object ( [animal] => crocodile [type] => REPTILE [animalCount] => 4 ) ) [BIRD] => Array ( [0] => stdClass Object ( [animal] => duck [type] => BIRD [animalCount] => 2 ) ) [MAMMAL] => Array ( [0] => stdClass Object ( [animal] => koala [type] => MAMMAL [animalCount] => 4 ) [1] => stdClass Object ( [animal] => lion [type] => MAMMAL [animalCount] => 5 ) ) ) */ For the same file lets parse with: - convert encoding from ISO-9959-1 to UTF-8 - convert lines to arrays - remove animals with count 0 - group by type $objectsArr = \Jupitern\Parser\FileParser::instance() ->setFile("csv.txt", ',') ->setEncoding('ISO-8859-1', 'UTF-8') ->filter(function ($line, $lineNumber) { return $lineNumber > 1 && $line[2] > 0; }) ->group(function ($line) { return $line[1]; }) ->parse(); echo '<pre>'; print_r($objectsArr); /* Output: Array ( [reptile] => Array ( [0] => Array ( [0] => crocodile [1] => reptile [2] => 4 ) ) [bird] => Array ( [0] => Array ( [0] => duck [1] => bird [2] => 2 ) ) [mammal] => Array ( [0] => Array ( [0] => koala [1] => mammal [2] => 4 ) [1] => Array ( [0] => lion [1] => mammal [2] => 5 ) ) ) */ Given a dsv file "file.txt" with contents (empolyee number, birth date, monthly income): 01john doe 1980-01-01 923.5 01luis west 1976-01-01 1143.3 01madalena 1983-01-01 2173.6 02Jaqueline Wayne 1983-01-01 822.44 05luís manuel 1983-01-01 1323.52 lets parse the file doing: - convert encoding from ISO-9959-1 to UTF-8 - convert lines to objects - format person name capitalize first letters - group by wage bellow or above 1000 $objectsArr = \Jupitern\Parser\FileParser::instance() ->setFile("test.txt") ->setEncoding('ISO-8859-1', 'UTF-8') ->each(function ($line){ $obj = []; $obj['Number'] = mb_substr($line, 0, 2); $obj['Name'] = mb_substr($line, 2, 16); $obj['BirthDate'] = mb_substr($line, 18, 10); $obj['MonthlyIncome'] = (float)mb_substr($line, 28, 15); return (object)$obj; }) ->format('Name', function ($val) { return ucwords($val); }) ->group(function ($line) { return (float)$line->MonthlyIncome >= 1000 ? 'above 1000' : 'bellow 1000'; }) ->parse(); echo '<pre>'; print_r($objectsArr); /* Output: Array ( [bellow 1000] => Array ( [0] => stdClass Object ( [Number] => 01 [Name] => John Doe [BirthDate] => 1980-01-01 [MonthlyIncome] => 923.5 ) [1] => stdClass Object ( [Number] => 02 [Name] => Jaqueline Wayne [BirthDate] => 1983-01-01 [MonthlyIncome] => 822.44 ) [2] => stdClass Object ( [Number] => 05 [Name] => LuÃs Manuel [BirthDate] => 1983-01-0 [MonthlyIncome] => 1 ) ) [above 1000] => Array ( [0] => stdClass Object ( [Number] => 01 [Name] => Luis West [BirthDate] => 1976-01-01 [MonthlyIncome] => 1143.3 ) [1] => stdClass Object ( [Number] => 01 [Name] => Madalena [BirthDate] => 1983-01-01 [MonthlyIncome] => 2173.6 ) ) ) */
ChangeLog
v1.2.0
- min php version updated to 8.0
- code refactor for php8
- allow parse from string or file
v1
- initial release
Contributing
- welcome to discuss a bugs, features and ideas.
License
jupitern/file-parser is release under the MIT license.
You are free to use, modify and distribute this software
jupitern/file-parser 适用场景与选型建议
jupitern/file-parser 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 13.93k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2017 年 02 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「csv」 「File parser」 「tsv」 「dsv」 「variable-length-delimited」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 jupitern/file-parser 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jupitern/file-parser 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 jupitern/file-parser 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Easy to use SDK with grabber for multiple platforms at once like YouTube, Dailymotion, Facebook and more.
The file manager intended for using Laravel with CKEditor / TinyMCE / Colorbox
An MT940 bank statement parser for PHP
Laravel integration for the jsonapi.org parser
Laravel Media Popup to upload/view the files
laravel facade to read/write csv file
统计信息
- 总下载量: 13.93k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 26
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-02-10