php读取torrent种子文件内容的方法(测试可用)
本文实例讲述了php读取torrent种子文件内容的方法。分享给大家供大家参考,具体如下:
<?php /** *ClassxBEncoder *Author:Angus.Fenying *Version:0.1 *Date:2014-06-03 * *ThisclasshelpsstringifyorparseBENC *codes. * *AllCopyrights2007-2014FenyingStudioReserved. */ classxBEncoder { constREADY=0; constREAD_STR=1; constREAD_DICT=2; constREAD_LIST=3; constREAD_INT=4; constREAD_KEY=5; public$y; protected$z,$m,$n; protected$stat; protected$stack; /** *Thismethodsavesthestatusofcurrent *encode/decodework. */ protectedfunctionpush($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); } /** *Thismethodrestorethesavedstatusofcurrent *encode/decodework. */ protectedfunctionpop() { $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; } } /** *Thismethodinitializesthestatusofwork. *YOUSHOULDCALLTHISMETHODBEFOREEVERYTHING. */ publicfunctioninit() { $this->stat=self::READY; $this->stack=array(); $this->z=$this->m=$this->n=0; } /** *Thismethoddecode$s($laslength). *Youcanget$obj->yastheresult. */ publicfunctiondecode($s,$l) { $this->y=0; for($i=0;$i<$l;++$i){ switch($this->stat){ caseself::READY: if($s[$i]=='d'){ $this->y=newxBDict(); $this->stat=self::READ_DICT; }elseif($s[$i]=='l'){ $this->y=array(); $this->stat=self::READ_LIST; } break; caseself::READ_INT: if($s[$i]=='e'){ $this->y->val=substr($s,$this->m,$i-$this->m); $this->pop(); } break; caseself::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; caseself::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; caseself::READ_DICT: if($s[$i]=='e'){ $this->pop(); break; }elseif(!$this->z){ $this->m=$i; $this->stat=self::READ_KEY; break; } caseself::READ_LIST: switch($s[$i]){ case'e': $this->pop(); break; case'd': $this->push(newxBDict(),self::READ_DICT); break; case'i': $this->push(newxBInt(),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; } /** *Thismethodencode$obj->yintoBEncode. */ publicfunctionencode() { return$this->_encDo($this->y); } protectedfunction_encStr($str) { returnstrlen($str).':'.$str; } protectedfunction_encDo($o) { if(is_string($o)) return$this->_encStr($o); if($oinstanceofxBInt) return'i'.$o->val.'e'; if($oinstanceofxBDict){ $r='d'; foreach($oas$k=>$c) $r.=$this->_encStr($k).$this->_encDo($c); return$r.'e'; } if(is_array($o)){ $r='l'; foreach($oas$c) $r.=$this->_encDo($c); return$r.'e'; } } } classxBDict { } classxBInt { public$val; publicfunction__construct($val=0) { $this->val=$val; } publicstaticfunctionisNum($chr) { $chr=ord($chr); if($chr<=57&&$chr>=48) returntrue; returnfalse; } } //使用实例 $s=file_get_contents("test.torrent"); $bc=newxBEncoder(); $bc->init(); $bc->decode($s,strlen($s)); var_dump($bc->y);
更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP数组(Array)操作技巧大全》、《PHP数学运算技巧总结》、《PHP图形与图片操作技巧汇总》、《php操作office文档技巧总结(包括word,excel,access,ppt)》、《php日期与时间用法总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。