PHP read torrent Seed file content method (test available), _php tutorial

Source: Internet
Author: User
Tags getting started with php

PHP read torrent The content of the seed file (test available),


This article describes how PHP reads the contents of a torrent seed file. Share to everyone for your reference, as follows:

<?php/** * Class Xbencoder * Author:Angus.Fenying * version:0.1 * date:2014-06-03 * * This Class helps stringify o R 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 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);

More readers interested in PHP related content can view the topic: "PHP array" operation Skills Daquan, "PHP Math Skills Summary", "PHP graphics and pictures Operation Skills Summary", "PHP operation Office Document tips summary (including Word,excel, access,ppt), "PHP Date and Time usage summary", "PHP primer for Object-oriented programming", "PHP String Usage Summary", "Getting Started with Php+mysql database operations" and "PHP Common Database Operations Skills Summary"

I hope this article is helpful to you in PHP programming.

http://www.bkjia.com/PHPjc/1125240.html www.bkjia.com true http://www.bkjia.com/PHPjc/1125240.html techarticle php read torrent The content of the seed file (test available), this article describes the PHP read torrent seed file content method. Share to everyone for reference, specific as follows: Php ...

  • 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.