Php method for reading torrent seed file (available for test) _ PHP

Source: Internet
Author: User
This article mainly introduces how php can read torrent seed file content. it can read and display torrent seed file content, which is simple and practical, for more information about how to use php to read torrent files, see the following example. We will share this with you for your reference. The details are as follows:

<? 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 shoshould 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_L IST;} 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; }}// use instance $ s = file_get_contents ("test. torrent "); $ bc = new xBEncoder (); $ bc-> init (); $ bc-> decode ($ s, strlen ($ s )); var_dump ($ bc-> y );

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.