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


Posted in PHP onMay 03, 2016

本文实例讲述了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 相关文章推荐
php将数据库中所有内容生成静态html文档的代码
Apr 12 PHP
并发下常见的加锁及锁的PHP具体实现代码
Oct 12 PHP
php 删除一个数组中的某个值.兼容多维数组!
Feb 18 PHP
无法在发生错误时创建会话,请检查 PHP 或网站服务器日志,并正确配置 PHP 安装(win+linux)
May 05 PHP
10个实用的PHP正则表达式汇总
Oct 23 PHP
getimagesize获取图片尺寸实例
Nov 15 PHP
php 把数字转换成汉字的代码
Jul 21 PHP
php析构函数的简单使用说明
Aug 24 PHP
Thinkphp5+plupload实现的图片上传功能示例【支持实时预览】
May 08 PHP
Laravel框架Eloquent ORM简介、模型建立及查询数据操作详解
Dec 04 PHP
PHP pthreads v3下worker和pool的使用方法示例
Feb 21 PHP
详解Go与PHP的语法对比
May 29 PHP
Yii2 输出xml格式数据的方法
May 03 #PHP
php面向对象值单例模式
May 03 #PHP
php使用ffmpeg获取视频信息并截图的实现方法
May 03 #PHP
Linux环境下php实现给网站截图的方法
May 03 #PHP
PHPExcel笔记, mpdf导出
May 03 #PHP
PHP实现的进度条效果详解
May 03 #PHP
php实现按天数、星期、月份查询的搜索框
May 02 #PHP
You might like
Zend Framework框架Smarty扩展实现方法
2016/03/22 PHP
slice函数的用法 之不错的应用
2006/12/29 Javascript
任意位置显示html菜单
2007/02/01 Javascript
关于jquery的多个选择器的使用示例
2013/10/18 Javascript
jQuery学习笔记之基础中的基础
2015/01/19 Javascript
Javascript对象Clone实例分析
2015/06/09 Javascript
js+html5实现canvas绘制椭圆形图案的方法
2016/05/21 Javascript
vuex实现简易计数器
2016/10/27 Javascript
JavaScript 轮播图和自定义滚动条配合鼠标滚轮分享代码贴
2016/10/28 Javascript
AngularJS过滤器filter用法分析
2016/12/11 Javascript
zTree实现节点修改的实时刷新功能
2017/03/20 Javascript
Vue中的基础过渡动画及实现原理解析
2018/12/04 Javascript
解决vue的过渡动画无法正常实现问题
2019/10/31 Javascript
python使用rabbitmq实现网络爬虫示例
2014/02/20 Python
python实现保存网页到本地示例
2014/03/16 Python
Python实现查找系统盘中需要找的字符
2015/07/14 Python
探究python中open函数的使用
2016/03/01 Python
python实现简单聊天应用 python群聊和点对点均实现
2017/09/14 Python
Python使用itchat模块实现简单的微信控制电脑功能示例
2019/08/26 Python
python实现删除列表中某个元素的3种方法
2020/01/15 Python
python图片剪裁代码(图片按四个点坐标剪裁)
2020/03/10 Python
详解利用python识别图片中的条码(pyzbar)及条码图片矫正和增强
2020/11/17 Python
python实现代码审查自动回复消息
2021/02/01 Python
利用html5 canvas破解简单验证码及getImageData接口应用
2013/01/25 HTML / CSS
英国领先的露营和露营车品牌之一:OLPRO
2019/08/06 全球购物
理工大学毕业生自荐信
2013/11/01 职场文书
计算机软件个人的自荐信范文
2013/12/01 职场文书
酒店个人培训自我鉴定
2013/12/11 职场文书
高一物理教学反思
2014/01/24 职场文书
《鸟的天堂》教学反思
2014/02/27 职场文书
文体活动总结范文
2014/05/05 职场文书
个人求职自荐信范文
2014/06/20 职场文书
2015年小学生国庆节演讲稿
2015/07/30 职场文书
执行力心得体会范文
2016/01/11 职场文书
创业计划书之酒厂
2019/10/14 职场文书
nginx前后端同域名配置的方法实现
2021/03/31 Servers