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切割页面div内容的实现代码分享
Jul 31 PHP
最常用的8款PHP调试工具
Jul 06 PHP
php中多维数组按指定value排序的实现代码
Aug 19 PHP
thinkphp中html:list标签传递多个参数实例
Oct 30 PHP
php实现指定字符串中查找子字符串的方法
Mar 17 PHP
PHP定时执行任务实现方法详解(Timer)
Jul 30 PHP
PHP中PDO的事务处理分析
Apr 07 PHP
php解决和避免form表单重复提交的几种方法
Aug 31 PHP
php支付宝系列之电脑网站支付
May 30 PHP
小程序微信退款功能实现方法详解【基于thinkPHP】
May 05 PHP
PHP从零开始打造自己的MVC框架之路由类实现方法分析
Jun 03 PHP
YII2 全局异常处理深入讲解
Mar 24 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
php cout&amp;lt;&amp;lt;的一点看法
2010/01/24 PHP
实现获取http内容的php函数分享
2014/02/16 PHP
PHP把小数转成整数3种方法
2014/06/30 PHP
php接口数据加密、解密、验证签名
2015/03/12 PHP
利用jQuery的deferred对象实现异步按顺序加载JS文件
2013/03/17 Javascript
iframe的onreadystatechange事件在firefox下的使用
2014/04/16 Javascript
JavaScript中的getTime()方法使用详解
2015/06/10 Javascript
jQuery图片轮播插件——前端开发必看
2016/05/31 Javascript
AngularJS 视图详解及示例代码
2016/08/17 Javascript
node+express框架中连接使用mysql(经验总结)
2018/11/10 Javascript
基于vue-cli3+typescript的tsx开发模板搭建过程分享
2020/02/28 Javascript
Python同时向控制台和文件输出日志logging的方法
2015/05/26 Python
Python中%r和%s的详解及区别
2017/03/16 Python
PIL图像处理模块paste方法简单使用详解
2019/07/17 Python
使用python模拟高斯分布例子
2019/12/09 Python
如何将PySpark导入Python的放实现(2种)
2020/04/26 Python
浅谈keras中的后端backend及其相关函数(K.prod,K.cast)
2020/06/29 Python
阿联酋电子产品购物网站:Menakart
2017/09/15 全球购物
欧洲最大的预定车位市场:JustPark
2020/01/06 全球购物
环境科学专业个人求职的自我评价
2013/11/28 职场文书
机电一体化专业推荐信
2013/12/03 职场文书
护士自荐信范文
2013/12/15 职场文书
服装设计师职业生涯规划范文
2014/02/28 职场文书
《搭石》教学反思
2014/04/07 职场文书
作风建设年活动总结
2014/08/27 职场文书
西岭雪山导游词
2015/02/06 职场文书
2015年度个人思想工作总结
2015/04/08 职场文书
刑事起诉书范文
2015/05/19 职场文书
教师节班会开场白
2015/06/01 职场文书
社会实践单位意见
2015/06/05 职场文书
三国演义读书笔记
2015/06/25 职场文书
2016领导干部廉洁从政心得体会
2016/01/19 职场文书
100句拼搏进取的名言警句,值得一读!
2019/10/07 职场文书
SQL试题 使用窗口函数选出连续3天登录的用户
2022/04/24 Oracle
MySQL 计算连续登录天数
2022/05/11 MySQL
使用Redis实现分布式锁的方法
2022/06/16 Redis