php讀取torrent種子文件內(nèi)容的方法(測(cè)試可用)
本文實(shí)例講述了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; } } //使用實(shí)例 $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é)運(yùn)算技巧總結(jié)》、《PHP圖形與圖片操作技巧匯總》、《php操作office文檔技巧總結(jié)(包括word,excel,access,ppt)》、《php日期與時(shí)間用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門(mén)教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門(mén)教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
- PHP批量獲取網(wǎng)頁(yè)中所有固定種子鏈接的方法
- PHP程序中的文件鎖、互斥鎖、讀寫(xiě)鎖使用技巧解析
- php讀取二進(jìn)制流(C語(yǔ)言結(jié)構(gòu)體struct數(shù)據(jù)文件)的深入解析
- PHP文件鎖定寫(xiě)入實(shí)例解析
- PHP實(shí)現(xiàn)將優(yōu)酷土豆騰訊視頻html地址轉(zhuǎn)換成flash swf地址的方法
- PHP基于新浪IP庫(kù)獲取IP詳細(xì)地址的方法
- php正則提取html圖片(img)src地址與任意屬性的方法
- php讀取qqwry.dat ip地址定位文件的類實(shí)例代碼
- THinkPHP獲取客戶端IP與IP地址查詢的方法
- php進(jìn)行ip地址掩碼運(yùn)算處理的方法
- PHP基于閉包思想實(shí)現(xiàn)的BT(torrent)文件解析工具實(shí)例詳解
相關(guān)文章
使用ThinkPHP框架(thinkphp8.0)創(chuàng)建定時(shí)任的操作步驟
這篇文章給大家介紹了使用ThinkPHP框架(thinkphp8.0)創(chuàng)建定時(shí)任的操作步驟,文中通過(guò)代碼示例給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-01-01基于HBase Thrift接口的一些使用問(wèn)題及相關(guān)注意事項(xiàng)的詳解
本篇文章是對(duì)HBase Thrift接口的一些使用問(wèn)題及相關(guān)注意事項(xiàng)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06php頁(yè)面消耗內(nèi)存過(guò)大的處理辦法
是因?yàn)閜hp頁(yè)面消耗的最大內(nèi)存默認(rèn)是為 8M (在PHP的ini件里可以看到) ,如果文件太大 或圖片太大 在讀取的時(shí)候 會(huì)發(fā)生錯(cuò)誤。2013-03-03PHP獲得當(dāng)日零點(diǎn)時(shí)間戳的方法分析
這篇文章主要介紹了PHP獲得當(dāng)日零點(diǎn)時(shí)間戳的方法,結(jié)合實(shí)例形式分析了php常見(jiàn)時(shí)間戳轉(zhuǎn)換與運(yùn)算相關(guān)操作技巧,需要的朋友可以參考下2018-03-03PHP實(shí)現(xiàn)獲取文件后綴名的幾種常用方法
這篇文章主要介紹了PHP實(shí)現(xiàn)獲取文件后綴名的幾種常用方法,通過(guò)三種不同的方法實(shí)例分析了php獲取文件后綴名的實(shí)現(xiàn)技巧,分別通過(guò)字符串、文件屬性及數(shù)組等方式實(shí)現(xiàn)這一功能,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08echo(),print(),print_r()之間的區(qū)別?
echo(),print(),print_r()之間的區(qū)別?...2006-11-11php和js實(shí)現(xiàn)根據(jù)子網(wǎng)掩碼和ip計(jì)算子網(wǎng)功能示例
這篇文章主要介紹了php和js實(shí)現(xiàn)根據(jù)子網(wǎng)掩碼和ip計(jì)算子網(wǎng)功能,結(jié)合實(shí)例形式分析了PHP與js針對(duì)IP地址子網(wǎng)掩碼計(jì)算的相關(guān)操作技巧,需要的朋友可以參考下2019-11-11