fanatique/php-fixed-length-file-parser
Composer 安装命令:
composer require fanatique/php-fixed-length-file-parser
包简介
A parser class for handling fixed length text files in PHP
关键字:
README 文档
README
A parser class for handling fixed length text files in PHP.
Fixed Length Files (aka poor man's CSV) are plain text files with one data set per row but without any delimiter:
01Amy BLUES
02Bob REDS
Installation
composer require fanatique/php-fixed-length-file-parser
Features
- Register a chopping map to define field positions and lengths
- Register a pre-flight check to filter lines before parsing
- Register a callback to transform each parsed line
- Supports any
callable(closures, invokable objects, static methods, etc.) - Memory-efficient line-by-line reading — safe for very large files
Usage
The following example shows how to transform a fixed length file into an associative array.
A working example can be found in example/parsing.php.
<?php declare(strict_types=1); require_once __DIR__ . '/vendor/autoload.php'; $parser = new \Fanatique\Parser\FixedLengthFileParser(); // Set the chopping map (aka where to extract the fields) $parser->setChoppingMap([ ['field_name' => 'id', 'start' => 0, 'length' => 2], ['field_name' => 'name', 'start' => 2, 'length' => 5, 'align' => 'left'], ['field_name' => 'team', 'start' => 7, 'length' => 5], ]); // Set the absolute path to the file $parser->setFilePath(__DIR__ . '/example.dat'); // Parse the file try { $parser->parse(); } catch (\Fanatique\Parser\ParserException $e) { echo 'ERROR - ' . $e->getMessage() . PHP_EOL; exit(1); } // Get the content var_dump($parser->getContent());
field_name and length are required. start is optional — if omitted, it is
calculated from the previous entry's start + length.
align is optional and configures which padding spaces to trim: 'left' (removes trailing), 'right' (removes leading), or 'both' (the default).
Registering a pre-flight check
A pre-flight check can be registered to filter each row before it is parsed. The callable receives the raw line as a string and must return a boolean:
true→ parse the linefalse→ skip the line
$parser->setPreflightCheck(function (string $line): bool { // Skip lines starting with a comment character return !str_starts_with($line, '#'); });
Registering a callback
A callback is applied to each line after parsing. It receives the parsed line as an associative array and must return an array of the same format:
$parser->setCallback(function (array $line): array { $line['team'] = ucwords(strtolower($line['team'])); return $line; });
Using invokable objects
Since the parser accepts any callable, you can use invokable objects for
more complex or reusable logic:
class TeamNormalizer { public function __invoke(array $line): array { $line['team'] = ucwords(strtolower($line['team'])); return $line; } } $parser->setCallback(new TeamNormalizer());
Development
# Install dependencies composer install # Run tests composer test # Run static analysis composer phpstan
License
MIT — see LICENSE for details.
fanatique/php-fixed-length-file-parser 适用场景与选型建议
fanatique/php-fixed-length-file-parser 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9.56k 次下载、GitHub Stars 达 15, 最近一次更新时间为 2012 年 11 月 15 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「text」 「parser」 「fixed-length」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 fanatique/php-fixed-length-file-parser 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 fanatique/php-fixed-length-file-parser 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 fanatique/php-fixed-length-file-parser 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Texy converts plain text in easy to read Texy syntax into structurally valid (X)HTML. It supports adding of images, links, nested lists, tables and has full support for CSS. Texy supports hyphenation of long words (which reflects language rules), clickable emails and URL (emails are obfuscated again
Easy to use SDK with grabber for multiple platforms at once like YouTube, Dailymotion, Facebook and more.
An MT940 bank statement parser for PHP
Common libraries used by Zimbra Api
Laravel integration for the jsonapi.org parser
A modern HTML DOM parser for PHP using DOMDocument and Symfony CssSelector.
统计信息
- 总下载量: 9.56k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 15
- 点击次数: 23
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2012-11-15