这篇文章主要介绍了php读取BT种子文件内容的方法,可实现读取并显示BT种子文件内容的功能,简单实用。需要的朋友可以参考下。希望对大家有所帮助。

具体如下:<?php

/**

* Class xBEncoder

* Author: Angus.Fenying

* Version: 0.1

*

* This class helps stringify or parse BENC

* codes.

*

* All Copyrights 2007 - 2014 Fenying Studio Reserved.

*/

class xBEncoder

{

const READY = 0;

const READ_STR = 1;

const READ_DICT = 2;

const READ_LIST = 3;

const READ_INT = 4;

const READ_KEY = 5;

public $y;

protected $z, $m, $n;

protected $stat;

protected $stack;

/**

* This method saves the status of current

* encode/decode work.

*/

protected function push($newY, $newStat)

{

array_push($this->stack, array($this->y, $this->z, $this->m, $this->n, $this->stat));

list($this->y, $this->z, $this->m, $this->n, $this->stat) = array($newY, 0, 0, 0, $newStat);

}

/**

* This method restore the saved status of current

* encode/decode work.

*/

protected function pop()

{

$t = array_pop($this->stack);

if ($t) {

if ($t[4] == self::READ_DICT) {

$t[0]->{$t[1]} = $this->y;

$t[1] = 0;

} elseif ($t[4] == self::READ_LIST)

$t[0][] = $this->y;

list($this->y, $this->z, $this->m, $this->n, $this->stat) = $t;

}

}

/**

* This method initializes the status of work.

* YOU SHOULD CALL THIS METHOD BEFORE EVERYTHING.

*/

public function init()

{

$this->stat = self::READY;

$this->stack = array();

$this->z = $this->m = $this->n = 0;

}

/**

* This method decode $s($l as length).

* You can get $obj->y as the result.

*/

public function decode($s, $l)

{

$this->y = 0;

for ($i = 0; $i < $l; ++$i) {

switch ($this->stat) {

case self::READY:

if ($s[$i] == 'd') {

$this->y = new xBDict();

$this->stat = self::READ_DICT;

} elseif ($s[$i] == 'l') {

$this->y = array();

$this->stat = self::READ_LIST;

}

break;

case self::READ_INT:

if ($s[$i] == 'e') {

$this->y->val = substr($s, $this->m, $i - $this->m);

$this->pop();

}

break;

case self::READ_STR:

if (xBInt::isNum($s[$i]))

continue;

if ($s[$i] = ':') {

$this->z = substr($s, $this->m, $i - $this->m);

$this->y = substr($s, $i + 1, $this->z + 0);

$i += $this->z;

$this->pop();

}

break;

case self::READ_KEY:

if (xBInt::isNum($s[$i]))

continue;

if ($s[$i] = ':') {

$this->n = substr($s, $this->m, $i - $this->m);

$this->z = substr($s, $i + 1, $this->n + 0);

$i += $this->n;

$this->stat = self::READ_DICT;

}

break;

case self::READ_DICT:

if ($s[$i] == 'e') {

$this->pop();

break;

} elseif (!$this->z) {

$this->m = $i;

$this->stat = self::READ_KEY;

break;

}

case self::READ_LIST:

switch ($s[$i]) {

case 'e':

$this->pop();

break;

case 'd':

$this->push(new xBDict(), self::READ_DICT);

break;

case 'i':

$this->push(new xBInt(), self::READ_INT);

$this->m = $i + 1;

break;

case 'l':

$this->push(array(), self::READ_LIST);

break;

default:

if (xBInt::isNum($s[$i])) {

$this->push('', self::READ_STR);

$this->m = $i;

}

}

break;

}

}

$rtn = empty($this->stack);

$this->init();

return $rtn;

}

/**

* This method encode $obj->y into BEncode.

*/

public function encode()

{

return $this->_encDo($this->y);

}

protected function _encStr($str)

{

return strlen($str) . ':' . $str;

}

protected function _encDo($o)

{

if (is_string($o))

return $this->_encStr($o);

if ($o instanceof xBInt)

return 'i' . $o->val . 'e';

if ($o instanceof xBDict) {

$r = 'd';

foreach ($o as $k => $c)

$r .= $this->_encStr($k) . $this->_encDo($c);

return $r . 'e';

}

if (is_array($o)) {

$r = 'l';

foreach ($o as $c)

$r .= $this->_encDo($c);

return $r . 'e';

}

}

}

class xBDict

{

}

class xBInt

{

public $val;

public function __construct($val = 0)

{

$this->val = $val;

}

public static function isNum($chr)

{

$chr = ord($chr);

if ($chr <= 57 && $chr >= 48)

return true;

return false;

}

}

//使用实例

$s = file_get_contents("test.torrent");

$bc = new xBEncoder();

$bc->init();

$bc->decode($s, strlen($s));

var_dump($bc->y);

相关推荐:

php获取种子失败,php读取BT种子文件内容的方法相关推荐

  1. 教大家python读取一行一行文件内容的方法

    Python中readline()每次读取文件中的一行,需要使用永真表达式循环读取文件.但当文件指针移动到文件的末尾时,依然使用readline()读取文件将出现错误.因此程序中需要添加1个判断语句, ...

  2. API读取写入 ini文件内容的方法函数详解

    ini文件(即Initialization file),这种类型的文件中通常存放的是一个程序的初始化信息.ini文件由若干个节(Section)组成,每个Section由若干键(Key)组成,每个Ke ...

  3. 种子读取php,php读取torrent种子文件内容的方法(测试可用),_PHP教程

    php读取torrent种子文件内容的方法(测试可用), 本文实例讲述了php读取torrent种子文件内容的方法.分享给大家供大家参考,具体如下: stack, array($this->y, ...

  4. php格式的种子,php读取torrent种子文件内容的方法(测试可用)

    本文实例讲述了php读取torrent种子文件内容的方法.分享给大家供大家参考,具体如下: /** * Class xBEncoder * Author: Angus.Fenying * Versio ...

  5. tp5 读取/下载 excel文件内容

    tp5 读取/下载 excel文件内容 1.下载一个excel模板 导出函数 /*** 创建(导出)Excel数据表格* @param array $expTableData 要导出的数组格式的数据* ...

  6. python 读取文件读出来是什么格式-深入学习python解析并读取PDF文件内容的方法...

    这篇文章主要学习了python解析并读取PDF文件内容的方法,包括对学习库的应用,python2.7和python3.6中python解析PDF文件内容库的更新,包括对pdfminer库的详细解释和应 ...

  7. python中读取文件内容-深入学习python解析并读取PDF文件内容的方法

    这篇文章主要学习了python解析并读取PDF文件内容的方法,包括对学习库的应用,python2.7和python3.6中python解析PDF文件内容库的更新,包括对pdfminer库的详细解释和应 ...

  8. js读取服务器txt文件,ZK中使用JS读取客户端txt文件内容问题

    最近写一个需求时遇到一个问题,用户需要通过点击一个按钮直接读取他自己电脑上D盘的一个txt文件内容显示到页面,因为项目现在是用ZK写的.我对于ZK也是刚刚了解不就,很多都还不是很熟.起初我是想用io流 ...

  9. python怎么读取pdf文件_Python解析并读取PDF文件内容的方法

    本文实例讲述了Python解析并读取PDF文件内容的方法.分享给大家供大家参考,具体如下: 一.问题描述 利用python,去读取pdf文本内容. 二.效果 三.运行环境 python2.7 四.需要 ...

最新文章

  1. Spark DataFrameDataSet
  2. 深度学习课程Deep Learning Courses
  3. 计算机毕业论文多久,计算机毕业论文
  4. 强烈的打击感jinbiguandan
  5. 【Proteus仿真8086】将IO接口电路封装成子电路CCT001
  6. vue 文字转语音mp3_vue项目或网站上实现文字转换成语音播放功能
  7. 2019腾讯广告算法大赛题目理解与数据探索(含代码)
  8. 找工作必做事项-剑指offer
  9. mysql join 性能测试_Mysql Join语法解析与性能分析
  10. Android基于Facebook Rebound的动画效果框架Backboard demo (非常炫酷)
  11. Python爬取哔哩哔哩实时直播弹幕
  12. 人工智能——可信度方法
  13. 获取字符串长度的几种办法
  14. [含论文+源码等]SSH超市进销存管理系统
  15. Unity ECS初学
  16. widerperson数据集转voc
  17. DApp公链选择需要考虑的问题
  18. 多个pdf文件合并为一个pdf文件
  19. ansys的kbc_在Ansys中施加阶跃载荷和渐变载荷的方法 | 坐倚北风
  20. 免费的ERP系统哪个好?这款让管理更高效

热门文章

  1. 干货|APP开发的需求分析步骤
  2. 如何快速接手一个 Java 项目
  3. 树莓派制作minecraft服务器,用树莓派搭建Minecraft服务器
  4. 计算机设备延长线缆传输距离,延长器
  5. redis续期_redis分布式锁自动延长过期时间
  6. RHCSA-A4.创建指定的用户账户
  7. Nginx接入链路监控SkyWalking
  8. 计算机毕业设计 SpringBoot+Vue拍卖竞拍系统 拍卖管理系统 网上拍卖系统 网络拍卖系统
  9. NoSQLBooster for MongoDB延长-试用期
  10. HUAWEI悦盒ec6108v9c 如何刷成海纳思系统(家用低功耗服务器,使用Home Assistant服务)