How PHP reads the contents of a BT seed file

Source: Internet
Author: User
Tags php file upload
This article mainly introduces the method of PHP to read the content of BT seed file, it can realize the function of reading and displaying the content of BT seed file, it is simple and practical. You need a friend to refer to below. We hope to help you.

Specific as follows:

<?php/** * Class Xbencoder * Author:Angus.Fenying * version:0.1 * * This Class helps stringify or parse Benc * code S. * * 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 the 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 to restore the saved status of the 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 the $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 <= && $chr >=) return true;  return false; }}//Use instance $s = file_get_contents ("test.torrent"); $BC = new Xbencoder (); $bc->init (); $BC->decode ($s, strlen ($s)); Var_dump ($BC->y);

Related recommendations:

PHP File Reading Series method

PHP File Upload principle of simple introduction

PHP File Cache function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.