欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

php读取torrent种子文件内容的方法(测试可用)

程序员文章站 2024-04-02 11:09:10
本文实例讲述了php读取torrent种子文件内容的方法。分享给大家供大家参考,具体如下:

本文实例讲述了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数组(array)操作技巧大全》、《php数学运算技巧总结》、《php图形与图片操作技巧汇总》、《php操作office文档技巧总结(包括word,excel,access,ppt)》、《php日期与时间用法总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家php程序设计有所帮助。