Skip to content

Commit fbd7d7f

Browse files
committed
Update README.md
1 parent 4086184 commit fbd7d7f

File tree

1 file changed

+40
-34
lines changed

1 file changed

+40
-34
lines changed

README.md

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,58 @@
11
Introduction
22
===
3-
The `php-bencode` is a PHP extension which can boost the process of encoding and decoding of [Bencode](https://en.wikipedia.org/wiki/Bencode). Supported by [PHP-CPP](https://github.com/CopernicaMarketingSoftware/PHP-CPP), the project only uses about 1000 lines of codes to implement the basic functions including encoding, decoding, loading, saving and etc.
3+
The `php-bencode` is a PHP extension which can boost the process of encoding and decoding of [Bencode](https://en.wikipedia.org/wiki/Bencode).
4+
5+
`php-bencode` now supports **only PHP 7** with no need of any external libraries. For PHP 5 support, please check the [php-5](https://github.com/Frederick888/php-bencode/tree/php-5) branch, which needs [PHP-CPP](https://github.com/CopernicaMarketingSoftware/PHP-CPP). (PHP 5 may be supported in the future, but it would take me a little long to get familiar with the old Zend APIs. So any PRs are welcomed.)
46

57
Installation
68
===
79
***Step 1*** Install dependencies
810
```bash
911
# Debian, Ubuntu (from launchpad)
10-
apt-get install php5-dev
12+
apt-get install php-dev
1113
# Redhat, CentOS, Fedora
1214
yum install php-devel
1315
```
14-
***Step 2*** Install PHP-CPP
15-
```bash
16-
git clone https://github.com/CopernicaMarketingSoftware/PHP-CPP.git
17-
cd PHP-CPP
18-
make -j$(nproc)
19-
make install
20-
```
21-
***Step 3*** Build and install
16+
17+
***Step 2*** Build and install
2218
```bash
2319
git clone https://git.tsundere.moe/Frederick888/php-bencode.git
2420
cd php-bencode
25-
make -j$(nproc)
21+
phpize
22+
./configure --enable-bencode
23+
make
2624
make install
2725
```
2826

2927
Basic Usage
3028
===
3129
***Example 1*** Parsing a string directly
3230
```
33-
php > $dict = BItem::parse("d4:key1l5:hello5:worlde4:key2i99ee");
34-
php > print_r($dict->toMetaArray());
31+
php > $bnode = bitem::parse("d4:key1l5:hello5:worlde4:key2i99ee");
32+
php > print_r($bnode->to_meta_array());
3533
Array
3634
(
37-
[_type] => BDict
35+
[_type] => bdict
36+
[_length] => 34
3837
[_data] => Array
3938
(
4039
[key1] => Array
4140
(
42-
[_type] => BList
41+
[_type] => blist
42+
[_length] => 16
4343
[_data] => Array
4444
(
4545
[0] => Array
4646
(
47-
[_type] => BStr
47+
[_type] => bstr
48+
[_length] => 7
4849
[_data] => hello
4950
)
5051
5152
[1] => Array
5253
(
53-
[_type] => BStr
54+
[_type] => bstr
55+
[_length] => 7
5456
[_data] => world
5557
)
5658
@@ -60,46 +62,50 @@ Array
6062
6163
[key2] => Array
6264
(
63-
[_type] => BInt
65+
[_type] => bint
66+
[_length] => 4
6467
[_data] => 99
6568
)
6669
6770
)
6871
6972
)
70-
php > $dict->set('key2', new BInt(100));
71-
php > echo $dict;
73+
php > $bnode->set('key2', new bint(100));
74+
php > echo $bnode;
7275
d4:key1l5:hello5:worlde4:key2i100ee
7376
```
7477
***Example 2*** Loading from/saving to a file
7578
```
76-
php > $dict = BItem::load("/path/sample.torrent");
77-
php > $dict->save("/path/sample_copy.torrent");
78-
php > echo md5_file("/path/sample.torrent") === md5_file("/path/sample_copy.torrent");
79-
1
79+
php > $bnode = bitem::load('/path/sample.dat');
80+
php > var_dump($bnode->save('/path/sample_copy.dat'));
81+
bool(true)
82+
php > var_dump(md5_file('/path/sample.dat') === md5_file('/path/sample_copy.dat'));
83+
bool(true)
8084
```
8185
***Example 3*** Set/delete a path
8286
```
83-
php > $dict = new BDict();
84-
php > $dict->set('key\/1/0', new BStr('hello')); // Escape the slash if you need it in a key
85-
php > $dict->set('key\/1/5', new BStr('world'));
86-
php > print_r($dict->toArray());
87+
php > $bnode = new bdict();
88+
php > $bnode->set('key1', new blist());
89+
php > $bnode->get('key1')->add(new bstr('hello'));
90+
php > $bnode->get('key1')->add(new bstr('world'));
91+
php > print_r($bnode->to_array());
8792
Array
8893
(
89-
[key/1] => Array
94+
[key1] => Array
9095
(
9196
[0] => hello
9297
[1] => world
9398
)
9499
95100
)
96-
php > echo $dict->del('key2'); // inexistent key
97-
php > echo $dict->del('key\/1/1');
98-
1
99-
php > print_r($dict->toArray());
101+
php > var_dump($bnode->del('key2')); // inexistent key
102+
bool(false)
103+
php > var_dump($bnode->get('key1')->del(1));
104+
bool(true)
105+
php > print_r($bnode->to_array());
100106
Array
101107
(
102-
[key/1] => Array
108+
[key1] => Array
103109
(
104110
[0] => hello
105111
)

0 commit comments

Comments
 (0)