fanatique/php-fixed-length-file-parser 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

fanatique/php-fixed-length-file-parser

Composer 安装命令:

composer require fanatique/php-fixed-length-file-parser

包简介

A parser class for handling fixed length text files in PHP

README 文档

README

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;
        }
);

统计信息

  • 总下载量: 8.81k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 15
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 15
  • Watchers: 2
  • Forks: 8
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2012-11-15