定制 themainframe/php-binary 二次开发

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

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

themainframe/php-binary

最新稳定版本:v0.2.0

Composer 安装命令:

composer require themainframe/php-binary

包简介

A PHP library for parsing structured binary streams.

README 文档

README

A PHP library for parsing structured binary streams.

Documentation

Documentation can be found in the documentation directory, as well as online at php-binary.damow.net

Usage

Here is an example binary format:

  1. 4 bytes of text.
  2. 1 byte unsigned integer.
  3. A field of 2 bytes of text followed by a 1 byte unsigned integer; repeated n times, where n is a backreference to the byte described in point 2.

Writing a Parser Schema

This format can be parsed as follows. In this example, the schema is described using JSON for clarity, though in practise any array may be used.

$builder = new Binary\SchemaBuilder;
$schema = $builder->createFromArray(json_decode('

    {
       "sometext": {
           "_type": "Text",
           "size": 4
       },
       "somebyte": {
           "_type": "UnsignedInteger",
           "size": 1
       },
       "somefields": {
           "_type": "Compound",
           "count": "@somebyte",
           "_fields": {
               "footext": {
                   "_type": "Text",
                   "size": 2
               },
               "foobyte": {
                   "_type": "UnsignedInteger",
                   "size": 1
               }
           }
       }
    }

', true));

Parsing a Stream

You can have php-binary parse a generic stream of bytes and output fields as an associative array matching your schema definition.

$stream = new Binary\Stream\StringStream("FOOO\x03LOLLOMLON");
$result = $schema->readStream($stream);

The resulting associative array in $result (shown here as JSON for clarity) would contain:

{
    "sometext": "FOOO",
    "somebyte": 3,
    "somefields": [
        {
            "footext": "LO",
            "foobyte": 76
        },
        {
            "footext": "LO",
            "foobyte": 77
        },
        {
            "footext": "LO",
            "foobyte": 78
        }
    ]
}

统计信息

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

GitHub 信息

  • Stars: 32
  • Watchers: 3
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-03-27