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 SQL防注入代码集合
Apr 25 PHP
php中使用explode查找某个字符是否存在的方法
Jul 12 PHP
常见php数据文件缓存类汇总
Dec 05 PHP
php抽象类使用要点与注意事项分析
Feb 09 PHP
php修改上传图片尺寸的方法
Apr 14 PHP
详解PHP的Yii框架中日志的相关配置及使用
Dec 08 PHP
几个优化WordPress中JavaScript加载体验的插件介绍
Dec 17 PHP
PHP简单计算两个时间差的方法示例
Jun 20 PHP
php 删除指定文件夹的实例讲解
Jul 25 PHP
PHP实现的策略模式简单示例
Aug 25 PHP
ThinkPHP中获取指定日期后工作日的具体日期方法
Oct 14 PHP
PHP利用递归函数实现无限级分类的方法
Mar 22 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
修改destoon会员公司的伪静态中的com目录的方法
2014/08/21 PHP
php实现的ping端口函数实例
2014/11/12 PHP
PHP创建对象的六种方式实例总结
2019/06/27 PHP
通过PHP的Wrapper无缝迁移原有项目到新服务的实现方法
2020/04/02 PHP
JS检测移动端横竖屏的代码
2016/05/30 Javascript
JavaScript中误用/g导致的正则test()无法正确重复执行的解决方案
2016/07/27 Javascript
微信小程序 利用css实现遮罩效果实例详解
2017/01/21 Javascript
jquery animate动画持续运动的实例
2017/11/29 jQuery
JavaScript时间戳与时间日期间相互转换
2017/12/11 Javascript
详解vue中localStorage的使用方法
2018/11/22 Javascript
学前端,css与javascript重难点浅析
2020/06/11 Javascript
python解决字典中的值是列表问题的方法
2013/03/04 Python
python实现代理服务功能实例
2013/11/15 Python
python求众数问题实例
2014/09/26 Python
Python3通过Luhn算法快速验证信用卡卡号的方法
2015/05/14 Python
Python程序中的观察者模式结构编写示例
2016/05/27 Python
Python基于回溯法子集树模板解决全排列问题示例
2017/09/07 Python
全面分析Python的优点和缺点
2018/02/07 Python
Python爬虫抓取代理IP并检验可用性的实例
2018/05/07 Python
Python 面试中 8 个必考问题
2018/11/16 Python
python计算n的阶乘的方法代码
2019/10/25 Python
django使用channels实现通信的示例
2020/10/19 Python
python设置中文界面实例方法
2020/10/27 Python
世界最大域名注册商:GoDaddy
2016/07/24 全球购物
美国体育用品商店:Academy Sports + Outdoors
2020/01/04 全球购物
软件测试企业面试试卷
2016/07/13 面试题
日语系毕业生推荐信
2013/11/11 职场文书
学生自我评价范文
2014/02/02 职场文书
大课间活动实施方案
2014/03/06 职场文书
快餐公司创业计划书
2014/04/29 职场文书
企业领导班子四风对照检查材料
2014/09/27 职场文书
2015年房地产销售工作总结
2015/04/20 职场文书
授权协议书范本(3篇)
2019/10/15 职场文书
python 如何用map()函数创建多线程任务
2021/04/07 Python
jquery插件实现悬浮的菜单
2021/04/24 jQuery
德劲DE1102数字调谐收音机机评
2022/04/07 无线电