PHPackages                             arhitector/zerg - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. arhitector/zerg

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

arhitector/zerg
===============

PHP structured binary parser

v0.2(9y ago)089MITPHPPHP &gt;=5.4.0

Since Feb 28Pushed 9y ago1 watchersCompare

[ Source](https://github.com/jack-theripper/zerg)[ Packagist](https://packagist.org/packages/arhitector/zerg)[ RSS](/packages/arhitector-zerg/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (1)Dependencies (2)Versions (4)Used By (0)

zerg [![Build Status](https://camo.githubusercontent.com/1181db33f659c16658284b48d832935ff0a4cf578e468adcd572b5fac6d5f4ea/68747470733a2f2f7472617669732d63692e6f72672f6b6c65726d6f6e74652f7a6572672e737667)](https://travis-ci.org/klermonte/zerg) [![Scrutinizer Code Quality](https://camo.githubusercontent.com/4e9bdc1d07aa7e6d4e18e8f3f29440b44c25d8a8fbf928a794d66228fa4457b7/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6b6c65726d6f6e74652f7a6572672f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/klermonte/zerg/?branch=master) [![Code Coverage](https://camo.githubusercontent.com/b80015cde5c9647a81ffc1028a283be7d162ad306258f3d04ec6174397be39e0/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6b6c65726d6f6e74652f7a6572672f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/klermonte/zerg/?branch=master)
=================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================

[](#zerg---)

Zerg is a small PHP tool that allow you simply parse structured binary files like lsdj memory dump file, jpeg encoded image or your custom binary format file.

Introdution
-----------

[](#introdution)

If you are reading this, chances are you know exactly why you need to read binary files in PHP. So I will not explain to you that this is not a good idea. Nevertheless, I like you needed to do this is in PHP. That's why I create zerg project. During creation, I was inspired by following projects: [alexras/bread](https://github.com/alexras/bread) and [themainframe/php-binary](https://github.com/themainframe/php-binary).

Installation
------------

[](#installation)

`composer require klermonte/zerg dev-master`
Or add `"klermonte/zerg": "dev-master"` to your dependancy list in composer.json and run `composer update`

Usage
-----

[](#usage)

```
// Describe your binary format in zerg language
$fieldCollection = new \Zerg\Field\Collection([
    'stringValue' => ['string', 15],
    'intValue' => ['arr', 5, ['int', 8]],
    'enumValue' => ['enum', 8, [
            0 => 'zero',
            10 => 'ten',
            32 => 'many'
        ], ['default' => 'not found']
    ]
]);

// Wrap your data in one of zerg streams
$sourceStream = new \Zerg\StringStream("Hello from zerg123456");

//Get your data structure
$data  = $fieldCollection->parse($sourceStream);
print_r($data);
/*
Array
(
    [stringValue] => Hello from zerg
    [intValue] => Array
        (
            [0] => 49
            [1] => 50
            [2] => 51
            [3] => 52
            [4] => 53
        )

    [enumValue] => not found
)
*/
```

Field types
-----------

[](#field-types)

### Integer

[](#integer)

```
// Object notation
// --------------------------------------
// $field = new Int(, );

$field = new Int(4);
$field = new Int('byte', [
    'signed' => true,
    'formatter' => function($value) {
        return $value * 100;
    }
]);

// Array notation
// --------------------------------------
// $fieldArray = ['int', , ];
```

Avaliable options

Option nameAvaliable valuesDescriptionsigned`boolean`, default `false`Whether field value is signed or notendian`PhpBio\Endian::ENDIAN_BIG` or
`PhpBio\Endian::ENDIAN_LITTLE`Endianess of fieldformatter`callable`callback, that take 2 arguments:
`function ($parsedValue, $dataSetInstance) {...}`### String

[](#string)

```
// Object notation
// --------------------------------------
// $field = new String(, );

$field = new String(16);
$field = new String('short', [
    'endian' => PhpBio\Endian::ENDIAN_BIG,
    'formatter' => function($value) {
        return str_repeat($value, 2);
    }
]);

// Array notation
// --------------------------------------
// $fieldArray = ['string', , ];
```

Avaliable options

Option nameAvaliable valuesDescriptionendian`PhpBio\Endian::ENDIAN_BIG` or
`PhpBio\Endian::ENDIAN_LITTLE`Endianess of fieldformatter`callable`callback, that take 2 arguments:
`function ($parsedValue, DataSet $dataSet) {...}`### Padding

[](#padding)

```
// Object notation
// --------------------------------------
// $field = new Padding();

$field = new Padding(16);

// Array notation
// --------------------------------------
// $fieldArray = ['padding', ];
```

### Enum

[](#enum)

```
// Object notation
// --------------------------------------
// $field = new Enum(, , );

$field = new Enum(8, [0, 1, 2, 3]);
$field = new Enum('short', [
        1234 => 'qwerty1',
        2345 => 'qwerty2'
    ], [
        'default' => 'abcdef'
    ]
);

// Array notation
// --------------------------------------
// $fieldArray = ['enum', , ];
```

Avaliable options

Option nameAvaliable valuesDescriptiondefault`mixed`, optionalValue, that will be returned, if no one key from `values` matchs to parsed valueAnd all options from **Integer** field type.

### Conditional

[](#conditional)

```
// Object notation
// --------------------------------------
// $field = new Conditional(, , );

$field = new Conditional('/path/to/key/value', [
        1 => ['int', 32],
        2 => ['string', 32]
    ], [
        'default' => ['padding', 32]
    ]
);

// Array notation
// --------------------------------------
// $fieldArray = ['conditional', , ];
```

Avaliable options

Option nameAvaliable valuesDescriptiondefault`array`, optionalField in array notation, that will be used, if no one key from `field` matchs to parsed value### Array

[](#array)

```
// Object notation
// --------------------------------------
// $field = new Arr(, , );

$field = new Arr(10, ['int', 32]);

// Array notation
// --------------------------------------
// $fieldArray = ['arr', , ];
```

Avaliable options

Option nameAvaliable valuesDescriptionuntil`'eof'` or `callable`If set, array field count parameter will be ignored, and field will parse values until End of File or callback return false, callback take one argument:
`function ($lastParsedValue) {...}`### Collection

[](#collection)

```
// Object notation
// --------------------------------------
// $field = new Collection(, );

$field = new Collection([
    'firstValue' => ['int', 32],
    'secondValue' => ['string', 32]
]);

// Array notation
// --------------------------------------
// $fieldArray = ['collection', , ];
// or just
// $fieldArray = ;
```

### Back links

[](#back-links)

Size, count and conditional key parameters may be declared as a back link - path to already parsed value. Path can starts with `/` sign, that means root of data set or with '../' for relative path.

```
$fieldCollection = new \Zerg\Field\Collection([
    'count' => ['string', 2],
    'intValue' => ['arr', '/count', ['int', 8]]
]);
$sourceStream = new \Zerg\StringStream("101234567890");
$data = $fieldCollection->parse($sourceStream);
print_r($data);
/*
Array
(
    [count] => 10
    [intValue] => Array
        (
            [0] => 49
            [1] => 50
            [2] => 51
            [3] => 52
            [4] => 53
            [5] => 54
            [6] => 55
            [7] => 56
            [8] => 57
            [9] => 48
        )
)
*/
```

### Conditional example

[](#conditional-example)

```
$fieldCollection = new \Zerg\Field\Collection([
    'count' => ['string', 2],
    'conditional' => ['conditional', '/count', [
            0 => ['string', 80],
            10 => ['int', 16]
        ],
        [
            'default' => ['string', 2]
        ]
    ]
]);
$sourceStream = new \Zerg\StringStream("101234567890");
$data = $fieldCollection->parse($sourceStream);
print_r($data);
/*
Array
(
    [count] => 10
    [conditional] => 12849
)
*/
```

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity50

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~330 days

Total

3

Last Release

3427d ago

Major Versions

v0.1 → 1.0.x-dev2015-04-04

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/5256163?v=4)[Arhitector](/maintainers/Arhitector)[@Arhitector](https://github.com/Arhitector)

---

Top Contributors

[![klermonte](https://avatars.githubusercontent.com/u/2529340?v=4)](https://github.com/klermonte "klermonte (97 commits)")

---

Tags

binary

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/arhitector-zerg/health.svg)

```
[![Health](https://phpackages.com/badges/arhitector-zerg/health.svg)](https://phpackages.com/packages/arhitector-zerg)
```

###  Alternatives

[mtdowling/jmespath.php

Declaratively specify how to extract elements from a JSON document

2.0k472.8M135](/packages/mtdowling-jmespathphp)[opis/closure

A library that can be used to serialize closures (anonymous functions) and arbitrary data.

2.6k230.0M282](/packages/opis-closure)[masterminds/html5

An HTML5 parser and serializer.

1.8k242.8M226](/packages/masterminds-html5)[sabberworm/php-css-parser

Parser for CSS Files written in PHP

1.8k191.2M63](/packages/sabberworm-php-css-parser)[jms/metadata

Class/method/property metadata management in PHP

1.8k152.8M88](/packages/jms-metadata)[jms/serializer-bundle

Allows you to easily serialize, and deserialize data of any complexity

1.8k89.3M618](/packages/jms-serializer-bundle)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
