PHP code example of zol / fixed-column-width-parser

1. Go to this page and download the library: Download zol/fixed-column-width-parser library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

zol / fixed-column-width-parser example snippets



use Zol\Parser\FixedColumnWidth\Parser;

$schema = [
    'entry' => [ 'name' => 10 ]
];

$parser = new Parser($schema, 'import.dat');
$parser->parse();

[
    // Ignored lines, null if none
    // First line is indexed by 1
    // Optionnal, null by default
    'ignore' => [1, 8 , 9],

    // Header line, null if missing
    // Optionnal, null by default
    'header' => ['field-name' => length, 'field-name' => length],

    // Define entry schema
    // Required
    'entry' => ['field-name' => length, 'field-name' => length],

    // Use header values as entry field names
    // If true, entry field names will be replaced with header values
    // Optionnal, false by default
    'header-as-field-name' => false,

    // Ignore empty line
    // Optionnal, true by default
    'ignore-empty-lines' => true,

    // Multiple files in one
    // If true, you must define separator
    // Optionnal, default false,
    'multiple' => false,

    // Separator, only used if multiple is true
    // Define files separator
    'separator' => [
        'field' => length, // Separator field
        'values' => [ 'value', 'value'], // Field values considered as separator
        'ignore' => true // Ignore separation line
    ]
]


use Zol\Parser\FixedColumnWidth\Parser;

$schema = [
    'ignore' => [2],
    'header' => [4, 9, 11, 10],
    'entry' => [4, 9, 11, 10],
    'header-as-field-name' => true
];

$parser = new Parser($schema, 'import.dat');
$parser->parse();

// Returns :
[
    'header' => [
        'id', 'name', 'phone', 'street'
    ],
    'entries' => [
        ['id' => '123', 'name' => 'john', 'phone' => '0152458652', 'street' => '5th street'],
        ['id' => '321', 'name' => 'isabelle', 'phone' => '0352158546', 'street' => '4th street']
    ]
]


use Zol\Parser\FixedColumnWidth\Parser;

$schema = [
    'ignore' => [2],
    'header' => [4, 9, 11, 10],
    'entry' => [4, 9, 11, 10],
    'header-as-field-name' => true,
    'multiple' => true,
    'separator' => [
        'field' => 4,
        'values' => ['id'],
        'ignore' => false
    ]
];

$parser = new Parser($schema, 'import-multiple.dat');
$parser->parse();

// Returns :
[
    [
        'header' => ['id', 'name', 'phone', 'street'],
        'entries' => [
            ['id' => '123', 'name' => 'john', 'phone' => '0152458652', 'street' => '5th street'],
            ['id' => '321', 'name' => 'isabelle', 'phone' => '0352158546', 'street' => '4th street']
        ]
    ],
    [
        'header' => ['id', 'name', 'phone_num', 'street'],
        'entries' => [
            ['id' => '123', 'name' => 'john', 'phone_num' => '0152458652', 'street' => '1st street'],
            ['id' => '321', 'name' => 'isabelle', 'phone_num' => '0352158546', 'street' => '2nd street']
        ]
    ],

]