bhutanio/bencode

Bencode 序列化器,用于 Laravel

2.0.2 2020-09-14 18:50 UTC

This package is auto-updated.

Last update: 2024-09-15 03:44:44 UTC


README

Latest Version on Packagist Software License Build Status Quality Score Total Downloads

此库允许开发者在 Laravel 中对 bencoded 数据字符串进行编码和解码。有关 bencode 的更多信息,请参阅 维基百科。该格式主要用于 .torrent 文件规范。

安装

通过 Composer

$ composer require bhutanio/bencode

使用方法

编码数组

<?php

use Bhutanio\Bencode\Bencode;

$data = array(
    "string" => "bar",
    "integer" => 42,
    "array" => array(
        "one",
        "two",
        "three",
    ),
);

echo Bencode::encode($data);

以上代码生成以下字符串:d5:arrayl3:one3:two5:threee7:integeri42e6:string3:bare

解码字符串

<?php

use Bhutanio\Bencode\Bencode;

$string = "d5:arrayl3:one3:two5:threee7:integeri42e6:string3:bare";

print_r(Bencode::decode($string));

以上代码生成以下输出

Array
(
    [array] => Array
        (
            [0] => one
            [1] => two
            [2] => three
        )

    [integer] => 42
    [string] => bar
)

测试

$ cp phpunit.xml.dst phpunit.xml
$ vendor/bin/phpunit -c phpunit.xml

许可协议

MIT 许可协议 (MIT)。有关更多信息,请参阅 许可文件