php讀取torrent種子文件內(nèi)容的方法(測(cè)試可用)
來(lái)源:易賢網(wǎng) 閱讀:1449 次 日期:2016-08-25 14:59:12
溫馨提示:易賢網(wǎng)小編為您整理了“php讀取torrent種子文件內(nèi)容的方法(測(cè)試可用)”,方便廣大網(wǎng)友查閱!

這篇文章主要介紹了php讀取torrent種子文件內(nèi)容的方法,可實(shí)現(xiàn)讀取并顯示torrent種子文件內(nèi)容的功能,簡(jiǎn)單實(shí)用,需要的朋友可以參考下

<?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;

  }

}

//使用實(shí)例

$s = file_get_contents("test.torrent");

$bc = new xBEncoder();

$bc->init();

$bc->decode($s, strlen($s));

var_dump($bc->y);

希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。

更多信息請(qǐng)查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機(jī)網(wǎng)站地址:php讀取torrent種子文件內(nèi)容的方法(測(cè)試可用)
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

2025國(guó)考·省考課程試聽報(bào)名

  • 報(bào)班類型
  • 姓名
  • 手機(jī)號(hào)
  • 驗(yàn)證碼
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 加入群交流 | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)