php-fwf

A fast, type-safe, language-agnostic compliant PHP library for parsing, formatting, and generating Fixed Width Files (FWF).

PHP 8.2+ / 8.5 PHPUnit 11 97%+ Coverage FWF Spec v1.0.0

⚡ Type-Safe Column Definitions

Support for CharColumn, RightCharColumn, PositiveIntegerColumn, PositiveDecimalColumn, DateColumn, TimeColumn, and DateTimeColumn.

📜 Structure Descriptors

Compose header, details, and footer rows using HeaderRowDescriptor, DetailRowDescriptor, and FooterRowDescriptor.

🔍 Cross-Language Hydration

Fully compatible with Python pyfwf and Java java-fwf JSON hydration formats and compliant with fwf-compliance-tests v1.0.0.

Quick Usage Example

<?php

use Kelsoncm\Fwf\Columns\CharColumn;
use Kelsoncm\Fwf\Columns\PositiveIntegerColumn;
use Kelsoncm\Fwf\Descriptors\DetailRowDescriptor;
use Kelsoncm\Fwf\Descriptors\FileDescriptor;
use Kelsoncm\Fwf\Readers\Reader;

// 1. Define Columns
$nameCol = new CharColumn('name', 20, 'User Name');
$ageCol = new PositiveIntegerColumn('age', 3, 'Age in years');

// 2. Define Descriptors
$detail = new DetailRowDescriptor([$nameCol, $ageCol]);
$fileDescriptor = new FileDescriptor([$detail]);

// 3. Read Content
$content = "KELSON MEDEIROS     045\nMARIA SILVA         030\n";
$reader = new Reader($content, $fileDescriptor, "\n");

foreach ($reader as $row) {
    echo "Name: {$row['name']} | Age: {$row['age']}\n";
}