dev-master
9999999-dev https://github.com/fanatique/php-fixed-length-file-parserA parser class for handling fixed length text files in PHP
MIT
The Requires
- php >=5.3.0
parser php text fixed length
A parser class for handling fixed length text files in PHP
A parser class for handling fixed length text files in PHP., (*1)
Fixed Length Files (aka poor man's CSV) are plain text files with one data set per row but without any delimiter., (*2)
01Amy BLUES 02Bob REDS ...
This class provides a rather comfortable way to handle this type of file on PHP., (*3)
You can:, (*4)
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
., (*5)
$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., (*6)
//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());
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:, (*7)
false
: line needs not to be parsedtrue
: parse line This example ignores any line which md5 sum is f23f81318ef24f1ba4df4781d79b7849
:, (*8)
$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; } );
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., (*9)
$parser->setCallback(function(array $currentLine) { $currentLine['team'] = ucwords(strtolower($currentLine['team'])); return $currentLine; } );
A parser class for handling fixed length text files in PHP
MIT
parser php text fixed length