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留言本实例代码
May 09 PHP
PHP 图片上传代码
Sep 13 PHP
基于命令行执行带参数的php脚本并取得参数的方法
Jan 25 PHP
Zend Framework教程之Application和Bootstrap用法详解
Mar 10 PHP
PHP通过微信跳转的Code参数获取用户的openid(关键代码)
Jul 06 PHP
PHP文件上传处理案例分析
Oct 15 PHP
php实现查询功能(数据访问)
May 23 PHP
PHP实现十进制数字与二十六进制字母串相互转换操作示例
Aug 10 PHP
网站被恶意镜像怎么办 php一段代码轻松搞定(全面版)
Oct 23 PHP
Laravel Eloquent ORM 实现查询表中指定的字段
Oct 17 PHP
KindEditor在php环境下上传图片功能集成的方法示例
Jul 20 PHP
php中array_fill函数的实例用法
Mar 02 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
获得Google PR值的PHP代码
2007/01/28 PHP
通过JavaScript或PHP检测Android设备的代码
2011/03/09 PHP
第4章 数据处理-php数组的处理-郑阿奇
2011/07/04 PHP
简单的php写入数据库类代码分享
2011/07/26 PHP
基于php下载文件的详解
2013/06/02 PHP
解析php根据ip查询所在地区(非常有用,赶集网就用到)
2013/07/01 PHP
Laravel中扩展Memcached缓存驱动实现使用阿里云OCS缓存
2015/02/10 PHP
PHP+Mysql+jQuery中国地图区域数据统计实例讲解
2015/10/10 PHP
PHP面相对象中的重载与重写
2017/02/13 PHP
PHP排序二叉树基本功能实现方法示例
2018/05/26 PHP
js 页面刷新location.reload和location.replace的区别小结
2009/12/24 Javascript
js 触发select onchange事件代码
2014/03/20 Javascript
Web表单提交之disabled问题js解决方法
2015/01/13 Javascript
纯javascript实现图片延时加载方法
2015/08/21 Javascript
JavaScript中Textarea滚动条不能拖动的解决方法
2015/12/15 Javascript
微信小程序 Video API实例详解
2016/10/02 Javascript
JavaScript利用正则表达式替换字符串中的内容
2016/12/12 Javascript
javascript 删除数组元素和清空数组的简单方法
2017/02/24 Javascript
Vue 实用分页paging实例代码
2017/04/12 Javascript
基于three.js实现的3D粒子动效实例代码
2019/04/09 Javascript
vue中监听路由参数的变化及方法
2019/12/06 Javascript
在vue中使用Echarts利用watch做动态数据渲染操作
2020/07/20 Javascript
Python字符串处理实例详解
2017/05/18 Python
pandas的唯一值、值计数以及成员资格的示例
2018/07/25 Python
pytorch 实现将自己的图片数据处理成可以训练的图片类型
2020/01/08 Python
高街生活方式全球在线商店:AZBRO
2017/08/26 全球购物
《陋室铭》教学反思
2014/02/26 职场文书
公证委托书格式
2014/09/13 职场文书
森马旗舰店双十一营销方案
2014/09/29 职场文书
列车乘务员工作不细心检讨书
2014/10/07 职场文书
社区党的群众路线教育实践活动剖析材料
2014/10/09 职场文书
2014小学语文教师个人工作总结
2014/12/03 职场文书
一年级数学上册复习计划
2015/01/17 职场文书
团结友爱主题班会
2015/08/13 职场文书
十二月早安励志心语大全
2019/12/03 职场文书
教你一步步实现一个简易promise
2021/11/02 Javascript