How PHP reads the contents of torrent seed files (test available) _php tips

Source: Internet
Author: User
Tags strlen

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

<?php/** * Class Xbencoder * Author:Angus.Fenying * version:0.1 * date:2014-06-03 * * This class helps Str
 Ingify 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-&G
    T;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 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-&
            GT;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-&GT;_ENCDO ($this->y); protected function _encstr ($STR) {return strlen ($STR). ':' .
  $STR;
    } protected function _encdo ($o) {if (is_string)) 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;
    The 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);

 

For more information about PHP interested readers can view the site topics: "PHP array" operation Skills Encyclopedia, "PHP Mathematical Calculation Skills Summary", "PHP graphics and pictures Operating skills summary", "PHP operation Office Document Skills Summary (including Word,excel, Access,ppt), "The PHP date and time usage summary", "PHP object-oriented Programming Introduction Tutorial", "PHP string (String) Usage Summary", "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation skill Summary"

I hope this article will help you with the PHP program design.

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.