Coder Social home page Coder Social logo

silverstripeltd / php-fixed-length-file-parser Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ssmarco/php-fixed-length-file-parser

0.0 1.0 0.0 20 KB

A parser class for handling fixed length text files in PHP

License: MIT License

PHP 100.00%

php-fixed-length-file-parser's Introduction

php-fixed-length-file-parser

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 
...

Features

This class provides a rather comfortable way to handle this type of file on PHP.

You can:

  • register a pre flight check to determine whether or not a row has to be parsed
  • register a callback to handle each line
  • register a chopping map which transforms each row into an assiciative array

Usage

The following example shows how to transform a fixed length file into an associative array. The working example can be found in example/parsing.php.

$parser = new \Fanatique\Parser\FixedLengthFileParser();

//Set the chopping map (aka where to extract the fields)
$parser->setChoppingMap(array(
  array('field_name' => 'id', 'start' => 0, 'length' => 2),
  array('field_name' => 'name', 'start' => 2, 'length' => 5),
  array('field_name' => 'team', 'start' => 7, 'length' => 5),
));

field_name and length are required and start is an optional parameter. If start is omitted, it will be set to the start plus length value of the previous map entry.

//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());

Registering a pre flight check

A pre flight check can be registered to be applied to each row before it is parsed. The closure needs to return a boolean value with:

  • false: line needs not to be parsed
  • true: parse line

This example ignores any line which md5 sum is f23f81318ef24f1ba4df4781d79b7849:

$linesToIgnore = array('f23f81318ef24f1ba4df4781d79b7849');
$parser->setPreflightCheck(function($currentLineStr) use($linesToIgnore) {
          if (in_array(md5($currentLineStr), $linesToIgnore)) {
              //Ignore line
              $ret = false;
          } else {
              //Parse line
              $ret = true;
          }
          return $ret;
      }
);

Registering a callback

Finally you can register a callback which is applied to each parsed line and allows you to process it. The closure gets the parsed line as an array and it is expected to return an array of the same format.

$parser->setCallback(function(array $currentLine) {
            $currentLine['team'] = ucwords(strtolower($currentLine['team']));
            return $currentLine;
        }
);

php-fixed-length-file-parser's People

Contributors

manuelbieh avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.