Bencodex reader/writer for PHP
This package implements Bencodex serialization format which extends Bencoding. Complianet with Bencodex 1.2.
php > echo Bencodex\encode(['foo' => 123, 'bar' => [true, false]]);
du3:barltfeu3:fooi123ee
php > var_dump(Bencodex\decode('du3:barltfeu3:fooi123ee'));
object(stdClass)#4 (2) {
["bar"]=>
array(2) {
[0]=>
bool(true)
[1]=>
bool(false)
}
["foo"]=>
int(123)
}
Requirements
- PHP 5.4 or later
- iconv extension (
--with-iconv
)
Type correspondences
PHP | Bencodex |
---|---|
Null | Null |
Boolean | Boolean |
Integer | Integer |
Double† | Integer (truncated) |
Numeric string‡ | Integer ≥ PHP_INT_MAX |
String which can be decoded as Unicode | Text‡ |
String otherwise | Binary‡ |
List-like array※ | List |
Map-like array† | Dictionary |
Object | Dictionary |
† One-way types only available for encoding.
‡ One-way types only available for decoding.
※ Determined by array_is_list()
function.
Usage
\Bencodex\encode(mixed $value): string
: Encodes a PHP$value
into Bencodex data.\Bencodex\decode(string $bencodex): mixed
: Decodes Bencodex data into a PHP value.
The above APIs are merely façade, and optional parameters are omitted. See the complete API docs as well.