• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

php读取BT种子文件内容的方法

php 搞代码 3年前 (2022-01-22) 12次浏览 已收录 0个评论

这篇文章主要介绍了php读取BT种子文件内容的方法,可实现读取并显示BT种子文件内容的功能,简单实用。需要的朋友可以参考下。希望对大家有所帮助。

具体如下:

<?php/** * Class xBEncoder * Author: Angus.Fenying * Version: 0.1 * *  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:        <mark style="color:transparent">本%文来源gaodaimacom搞#^代%!码网@</mark>搞代gaodaima码  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 文件缓存函数

以上就是php读取BT种子文件内容的方法的详细内容,更多请关注搞代码gaodaima其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php读取BT种子文件内容的方法
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址