php读取torrent种子文件内容的方法(测试可用)

 更新时间:2016年5月5日 10:11  点击:1442

本文实例讲述了php读取torrent种子文件内容的方法。分享给大家供大家参考,具体如下:

<?php
/**
 * Class xBEncoder
 * Author: Angus.Fenying
 * Version: 0.1
 * Date:  2014-06-03
 *
 *  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数组(Array)操作技巧大全》、《PHP数学运算技巧总结》、《PHP图形与图片操作技巧汇总》、《php操作office文档技巧总结(包括word,excel,access,ppt)》、《php日期与时间用法总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

[!--infotagslink--]

相关文章

  • 源码分析系列之json_encode()如何转化一个对象

    这篇文章主要介绍了源码分析系列之json_encode()如何转化一个对象,对json_encode()感兴趣的同学,可以参考下...2021-04-22
  • php中去除文字内容中所有html代码

    PHP去除html、css样式、js格式的方法很多,但发现,它们基本都有一个弊端:空格往往清除不了 经过不断的研究,最终找到了一个理想的去除html包括空格css样式、js 的PHP函数。...2013-08-02
  • index.php怎么打开?如何打开index.php?

    index.php怎么打开?初学者可能不知道如何打开index.php,不会的同学可以参考一下本篇教程 打开编辑:右键->打开方式->经文本方式打开打开运行:首先你要有个支持运行PH...2017-07-06
  • PHP中func_get_args(),func_get_arg(),func_num_args()的区别

    复制代码 代码如下:<?php function jb51(){ print_r(func_get_args()); echo "<br>"; echo func_get_arg(1); echo "<br>"; echo func_num_args(); } jb51("www","j...2013-10-04
  • PHP编程 SSO详细介绍及简单实例

    这篇文章主要介绍了PHP编程 SSO详细介绍及简单实例的相关资料,这里介绍了三种模式跨子域单点登陆、完全跨单点域登陆、站群共享身份认证,需要的朋友可以参考下...2017-01-25
  • PHP实现创建以太坊钱包转账等功能

    这篇文章主要介绍了PHP实现创建以太坊钱包转账等功能,对以太坊感兴趣的同学,可以参考下...2021-04-20
  • php微信公众账号开发之五个坑(二)

    这篇文章主要为大家详细介绍了php微信公众账号开发之五个坑,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2016-10-02
  • PHP如何通过date() 函数格式化显示时间

    这篇文章主要介绍了PHP如何通过date() 函数格式化显示时间,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-11-13
  • ThinkPHP使用心得分享-ThinkPHP + Ajax 实现2级联动下拉菜单

    首先是数据库的设计。分类表叫cate.我做的是分类数据的二级联动,数据需要的字段有:id,name(中文名),pid(父id). 父id的设置: 若数据没有上一级,则父id为0,若有上级,则父id为上一级的id。数据库有内容后,就可以开始写代码,进...2014-05-31
  • golang与php实现计算两个经纬度之间距离的方法

    这篇文章主要介绍了golang与php实现计算两个经纬度之间距离的方法,结合实例形式对比分析了Go语言与php进行经纬度计算的相关数学运算技巧,需要的朋友可以参考下...2016-07-29
  • PHP+jQuery+Ajax实现多图片上传效果

    今天我给大家分享的是在不刷新页面的前提下,使用PHP+jQuery+Ajax实现多图片上传的效果。用户只需要点击选择要上传的图片,然后图片自动上传到服务器上并展示在页面上。...2015-03-15
  • PHP正则表达式过滤html标签属性(DEMO)

    这篇文章主要介绍了PHP正则表达式过滤html标签属性的相关内容,实用性非常,感兴趣的朋友参考下吧...2016-05-06
  • php构造方法中析构方法在继承中的表现

    这篇文章主要为大家详细介绍了php构造方法中析构方法在继承中的表现,感兴趣的小伙伴们可以参考一下...2016-04-15
  • PHP如何使用cURL实现Get和Post请求

    这篇文章主要介绍了PHP如何使用cURL实现Get和Post请求,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-07-11
  • thinkPHP中多维数组的遍历方法

    这篇文章主要介绍了thinkPHP中多维数组的遍历方法,以简单实例形式分析了thinkPHP中foreach语句的使用技巧,需要的朋友可以参考下...2016-01-12
  • 谈谈PHP中相对路径的问题与绝对路径的使用

    经常看到有人踩在了PHP路径的坑上面了,感觉有必要来说说PHP中相对路径的一些坑,以及PHP中绝对路径的使用,下面一起来看看。 ...2016-08-24
  • php有序列表或数组中删除指定的值的实现代码

    这篇文章主要介绍了php有序列表或数组中删除指定的值的实现代码,删除给定的值之后,得到一个新的有序列表,长度-1,下面是具体的实现方法...2021-08-22
  • PHP简单实现生成txt文件到指定目录的方法

    这篇文章主要介绍了PHP简单实现生成txt文件到指定目录的方法,简单对比分析了PHP中fwrite及file_put_contents等函数的使用方法,需要的朋友可以参考下...2016-04-28
  • php判断邮箱地址是否存在的方法

    这篇文章主要介绍了php判断邮箱地址是否存在的方法,php判断邮箱地址是否存在的方法有两种,感兴趣的朋友可以参考一下...2016-02-18
  • php图片添加文字水印实现代码

    这篇文章主要为大家详细介绍了php图片添加文字水印实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2016-03-17