定制 gugglegum/clv-rw 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

gugglegum/clv-rw

Composer 安装命令:

composer require gugglegum/clv-rw

包简介

Reader and writer for text files with constant length values

README 文档

README

CLV is a text-table format with fixed-width cells and no delimiter character. Each row is a text line separated by LF or CRLF (\n or \r\n). Each cell has a predefined character width, and missing characters are padded with spaces by default.

This format is mostly useful when integrating with older systems. For new services, CSV or JSON is usually a better choice. The main limitation is that values cannot be longer than their configured column width unless you explicitly enable trimming.

The library can work with both files and streams, so it can read from STDIN, write to STDOUT, or work with temporary streams such as php://temp.

Column widths are measured in UTF-8 characters, not bytes.

Requirements

  • PHP 8.3 or newer
  • ext-mbstring

Installation

composer require gugglegum/clv-rw

Reading

use gugglegum\ClvRw\Column;
use gugglegum\ClvRw\ColumnsSet;
use gugglegum\ClvRw\Reader;

$columns = new ColumnsSet([
    Column::create('Code', 4),
    Column::create('Name', 10),
]);

$reader = (new Reader())->open('input.clv', $columns);

foreach ($reader as $row) {
    echo $row['Code'], ': ', $row['Name'], PHP_EOL;
}

$reader->close();

Writing

use gugglegum\ClvRw\Column;
use gugglegum\ClvRw\ColumnsSet;
use gugglegum\ClvRw\Writer;

$columns = new ColumnsSet([
    Column::create('Code', 4),
    Column::create('Name', 10),
]);

$writer = (new Writer())->open('output.clv', $columns);
$writer->writeRow(['Code' => 'A1', 'Name' => 'Widget']);
$writer->close();

Writer::open() opens the target file in write mode and truncates existing contents. Use Writer::assign() if you need to write to an already opened stream, for example STDOUT or php://temp.

By default, values that exceed their column width throw an exception. You can trim them instead:

$writer->setTrimTooLongValues(true);

Examples

See the example/ directory:

php example/reader.php
php example/reader.php | php example/writer.php

Line Endings

The reader handles LF and CRLF line endings. Classic Mac CR-only files should be normalized before reading. PHP's auto_detect_line_endings option is deprecated, so it is better not to rely on it for new code.

Exceptions

Package errors are thrown as gugglegum\ClvRw\Exception, which extends PHP's RuntimeException.

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-11-26

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固