承接 fanatique/php-fixed-length-file-parser 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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

PHP Version CI License: MIT

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 line
  • false → 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 我们能提供哪些服务?
定制开发 / 二次开发

基于 fanatique/php-fixed-length-file-parser 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 9.56k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 15
  • 点击次数: 23
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 15
  • Watchers: 2
  • Forks: 8
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2012-11-15