php讀取torrent種子文件內(nèi)容的方法(測試可用)
本文實例講述了php讀取torrent種子文件內(nèi)容的方法。分享給大家供大家參考,具體如下:
<?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);
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP數(shù)組(Array)操作技巧大全》、《PHP數(shù)學(xué)運算技巧總結(jié)》、《PHP圖形與圖片操作技巧匯總》、《php操作office文檔技巧總結(jié)(包括word,excel,access,ppt)》、《php日期與時間用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
- PHP批量獲取網(wǎng)頁中所有固定種子鏈接的方法
- PHP程序中的文件鎖、互斥鎖、讀寫鎖使用技巧解析
- php讀取二進制流(C語言結(jié)構(gòu)體struct數(shù)據(jù)文件)的深入解析
- PHP文件鎖定寫入實例解析
- PHP實現(xiàn)將優(yōu)酷土豆騰訊視頻html地址轉(zhuǎn)換成flash swf地址的方法
- PHP基于新浪IP庫獲取IP詳細地址的方法
- php正則提取html圖片(img)src地址與任意屬性的方法
- php讀取qqwry.dat ip地址定位文件的類實例代碼
- THinkPHP獲取客戶端IP與IP地址查詢的方法
- php進行ip地址掩碼運算處理的方法
- PHP基于閉包思想實現(xiàn)的BT(torrent)文件解析工具實例詳解
相關(guān)文章
并發(fā)下常見的加鎖及鎖的PHP具體實現(xiàn)代碼
用到了Eaccelerator的內(nèi)存鎖 和 文件鎖,原理如下判斷系統(tǒng)中是否安了EAccelerator 如果有則使用內(nèi)存鎖,如果不存在,則進行文件鎖2010-10-10
php外部執(zhí)行命令函數(shù)用法小結(jié)
這篇文章主要介紹了php外部執(zhí)行命令函數(shù)用法,結(jié)合實例形式分析了exec與system執(zhí)行外部命令的相關(guān)使用技巧,需要的朋友可以參考下2016-10-10
PHP實現(xiàn)找出數(shù)組中出現(xiàn)次數(shù)超過數(shù)組長度一半的數(shù)字算法示例
這篇文章主要介紹了PHP實現(xiàn)找出數(shù)組中出現(xiàn)次數(shù)超過數(shù)組長度一半的數(shù)字算法,涉及php數(shù)組的遍歷、統(tǒng)計、判斷等相關(guān)操作技巧,需要的朋友可以參考下2017-10-10
php過濾htmlspecialchars() 函數(shù)實現(xiàn)把預(yù)定義的字符轉(zhuǎn)換為 HTML 實體用法分析
這篇文章主要介紹了php過濾htmlspecialchars() 函數(shù)實現(xiàn)把預(yù)定義的字符轉(zhuǎn)換為 HTML 實體用法,結(jié)合實例形式分析了htmlspecialchars()函數(shù)針對HTML進行字符轉(zhuǎn)義的相關(guān)操作技巧,需要的朋友可以參考下2019-06-06

