登录  /  注册

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

*文
发布: 2017-12-29 18:59:15
原创
3303人浏览过

这篇文章主要介绍了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] == &#39;d&#39;) {
            $this->y = new xBDict();
            $this->stat = self::READ_DICT;
          } elseif ($s[$i] == &#39;l&#39;) {
            $this->y = array();
            $this->stat = self::READ_LIST;
          }
          break;
        case self::READ_INT:
          if ($s[$i] == &#39;e&#39;) {
            $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] = &#39;:&#39;) {
            $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] = &#39;:&#39;) {
            $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] == &#39;e&#39;) {
            $this->pop();
            break;
          } elseif (!$this->z) {
            $this->m = $i;
            $this->stat = self::READ_KEY;
            break;
          }
        case self::READ_LIST:
          switch ($s[$i]) {
            case &#39;e&#39;:
              $this->pop();
              break;
            case &#39;d&#39;:
              $this->push(new xBDict(), self::READ_DICT);
              break;
            case &#39;i&#39;:
              $this->push(new xBInt(), self::READ_INT);
              $this->m = $i + 1;
              break;
            case &#39;l&#39;:
              $this->push(array(), self::READ_LIST);
              break;
            default:
              if (xBInt::isNum($s[$i])) {
                $this->push(&#39;&#39;, 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) . &#39;:&#39; . $str;
  }
  protected function _encDo($o)
  {
    if (is_string($o))
      return $this->_encStr($o);
    if ($o instanceof xBInt)
      return &#39;i&#39; . $o->val . &#39;e&#39;;
    if ($o instanceof xBDict) {
      $r = &#39;d&#39;;
      foreach ($o as $k => $c)
        $r .= $this->_encStr($k) . $this->_encDo($c);
      return $r . &#39;e&#39;;
    }
    if (is_array($o)) {
      $r = &#39;l&#39;;
      foreach ($o as $c)
        $r .= $this->_encDo($c);
      return $r . &#39;e&#39;;
    }
  }
}
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种子文件内容的方法的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号