A fast, type-safe, language-agnostic compliant PHP library for parsing, formatting, and generating Fixed Width Files (FWF).
Support for CharColumn, RightCharColumn, PositiveIntegerColumn, PositiveDecimalColumn, DateColumn, TimeColumn, and DateTimeColumn.
Compose header, details, and footer rows using HeaderRowDescriptor, DetailRowDescriptor, and FooterRowDescriptor.
Fully compatible with Python pyfwf and Java java-fwf JSON hydration formats and compliant with fwf-compliance-tests v1.0.0.
<?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";
}