php讀取torrent種子檔案內容的方法(測試可用),_PHP教程

來源:互聯網
上載者:User

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程式設計有所協助。

http://www.bkjia.com/PHPjc/1125240.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1125240.htmlTechArticlephp讀取torrent種子檔案內容的方法(測試可用), 本文執行個體講述了php讀取torrent種子檔案內容的方法。分享給大家供大家參考,具體如下: php...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.