当前位置:Gxlcms > PHP教程 > php读取torrent种子文件内容的方法(测试可用)_PHP

php读取torrent种子文件内容的方法(测试可用)_PHP

时间:2021-07-01 10:21:17 帮助过:12人阅读

本文实例讲述了php读取torrent种子文件内容的方法。分享给大家供大家参考,具体如下:

  1. <?php
  2. /**
  3. * Class xBEncoder
  4. * Author: Angus.Fenying
  5. * Version: 0.1
  6. * Date: 2014-06-03
  7. *
  8. * This class helps stringify or parse BENC
  9. * codes.
  10. *
  11. * All Copyrights 2007 - 2014 Fenying Studio Reserved.
  12. */
  13. class xBEncoder
  14. {
  15. const READY = 0;
  16. const READ_STR = 1;
  17. const READ_DICT = 2;
  18. const READ_LIST = 3;
  19. const READ_INT = 4;
  20. const READ_KEY = 5;
  21. public $y;
  22. protected $z, $m, $n;
  23. protected $stat;
  24. protected $stack;
  25. /**
  26. * This method saves the status of current
  27. * encode/decode work.
  28. */
  29. protected function push($newY, $newStat)
  30. {
  31. array_push($this->stack, array($this->y, $this->z, $this->m, $this->n, $this->stat));
  32. list($this->y, $this->z, $this->m, $this->n, $this->stat) = array($newY, 0, 0, 0, $newStat);
  33. }
  34. /**
  35. * This method restore the saved status of current
  36. * encode/decode work.
  37. */
  38. protected function pop()
  39. {
  40. $t = array_pop($this->stack);
  41. if ($t) {
  42. if ($t[4] == self::READ_DICT) {
  43. $t[0]->{$t[1]} = $this->y;
  44. $t[1] = 0;
  45. } elseif ($t[4] == self::READ_LIST)
  46. $t[0][] = $this->y;
  47. list($this->y, $this->z, $this->m, $this->n, $this->stat) = $t;
  48. }
  49. }
  50. /**
  51. * This method initializes the status of work.
  52. * YOU SHOULD CALL THIS METHOD BEFORE EVERYTHING.
  53. */
  54. public function init()
  55. {
  56. $this->stat = self::READY;
  57. $this->stack = array();
  58. $this->z = $this->m = $this->n = 0;
  59. }
  60. /**
  61. * This method decode $s($l as length).
  62. * You can get $obj->y as the result.
  63. */
  64. public function decode($s, $l)
  65. {
  66. $this->y = 0;
  67. for ($i = 0; $i < $l; ++$i) {
  68. switch ($this->stat) {
  69. case self::READY:
  70. if ($s[$i] == 'd') {
  71. $this->y = new xBDict();
  72. $this->stat = self::READ_DICT;
  73. } elseif ($s[$i] == 'l') {
  74. $this->y = array();
  75. $this->stat = self::READ_LIST;
  76. }
  77. break;
  78. case self::READ_INT:
  79. if ($s[$i] == 'e') {
  80. $this->y->val = substr($s, $this->m, $i - $this->m);
  81. $this->pop();
  82. }
  83. break;
  84. case self::READ_STR:
  85. if (xBInt::isNum($s[$i]))
  86. continue;
  87. if ($s[$i] = ':') {
  88. $this->z = substr($s, $this->m, $i - $this->m);
  89. $this->y = substr($s, $i + 1, $this->z + 0);
  90. $i += $this->z;
  91. $this->pop();
  92. }
  93. break;
  94. case self::READ_KEY:
  95. if (xBInt::isNum($s[$i]))
  96. continue;
  97. if ($s[$i] = ':') {
  98. $this->n = substr($s, $this->m, $i - $this->m);
  99. $this->z = substr($s, $i + 1, $this->n + 0);
  100. $i += $this->n;
  101. $this->stat = self::READ_DICT;
  102. }
  103. break;
  104. case self::READ_DICT:
  105. if ($s[$i] == 'e') {
  106. $this->pop();
  107. break;
  108. } elseif (!$this->z) {
  109. $this->m = $i;
  110. $this->stat = self::READ_KEY;
  111. break;
  112. }
  113. case self::READ_LIST:
  114. switch ($s[$i]) {
  115. case 'e':
  116. $this->pop();
  117. break;
  118. case 'd':
  119. $this->push(new xBDict(), self::READ_DICT);
  120. break;
  121. case 'i':
  122. $this->push(new xBInt(), self::READ_INT);
  123. $this->m = $i + 1;
  124. break;
  125. case 'l':
  126. $this->push(array(), self::READ_LIST);
  127. break;
  128. default:
  129. if (xBInt::isNum($s[$i])) {
  130. $this->push('', self::READ_STR);
  131. $this->m = $i;
  132. }
  133. }
  134. break;
  135. }
  136. }
  137. $rtn = empty($this->stack);
  138. $this->init();
  139. return $rtn;
  140. }
  141. /**
  142. * This method encode $obj->y into BEncode.
  143. */
  144. public function encode()
  145. {
  146. return $this->_encDo($this->y);
  147. }
  148. protected function _encStr($str)
  149. {
  150. return strlen($str) . ':' . $str;
  151. }
  152. protected function _encDo($o)
  153. {
  154. if (is_string($o))
  155. return $this->_encStr($o);
  156. if ($o instanceof xBInt)
  157. return 'i' . $o->val . 'e';
  158. if ($o instanceof xBDict) {
  159. $r = 'd';
  160. foreach ($o as $k => $c)
  161. $r .= $this->_encStr($k) . $this->_encDo($c);
  162. return $r . 'e';
  163. }
  164. if (is_array($o)) {
  165. $r = 'l';
  166. foreach ($o as $c)
  167. $r .= $this->_encDo($c);
  168. return $r . 'e';
  169. }
  170. }
  171. }
  172. class xBDict
  173. {
  174. }
  175. class xBInt
  176. {
  177. public $val;
  178. public function __construct($val = 0)
  179. {
  180. $this->val = $val;
  181. }
  182. public static function isNum($chr)
  183. {
  184. $chr = ord($chr);
  185. if ($chr <= 57 && $chr >= 48)
  186. return true;
  187. return false;
  188. }
  189. }
  190. //使用实例
  191. $s = file_get_contents("test.torrent");
  192. $bc = new xBEncoder();
  193. $bc->init();
  194. $bc->decode($s, strlen($s));
  195. var_dump($bc->y);

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP数组(Array)操作技巧大全》、《PHP数学运算技巧总结》、《PHP图形与图片操作技巧汇总》、《php操作office文档技巧总结(包括word,excel,access,ppt)》、《php日期与时间用法总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

人气教程排行