function count_down($count)
{
return $func = function()
use($count,$func)
{
if(--$count > 0)
$func();
echo "wow\n";
};
}
$foo = count_down(3);
$foo();
function count_down($count)
{
return $foo = function()
use(&$count,&$foo)
{
echo $count."\n";
if(--$count > 0)
$foo();
};
}
$foo = count_down(4);
$foo();
function count_down_again($count)
{
return function()use($count)
{
printf("wow %d\n",$count);
return --$count;
};
}
$foo = count_down_again(5);
while($foo() >0);
function count_down_again($count)
{
return function()use(&$count)
{
printf("wow %d\n",$count);
return --$count;
};
}
$foo = count_down_again(5);
while($foo() >0);
class Foo
{
public function __invoke($count)
{
if($count > 0)
$this($count - 1);
echo "wow\n";
}
}
$foo = new Foo();
$foo(4);
function yet_another_count_down($func,$count)
{
$func($count);
if($count > 0)
yet_another_count_down($func,$count - 1);
}
yet_another_count_down(function($var){echo $var."\n";},6);
<?php
$file_name = '1.torrent';
$file = fopen($file_name,'r');
$nil = new Parser($file);//构造解析器
$nil = $nil();//进行解析
$pos = ftell($file);
echo '读取到文件位置'.sprintf('0xX',$pos)."\r\n";
fseek($file,0,SEEK_END);
echo '还剩下'.(ftell($file) - $pos).'字节未读取'."\r\n";
if(!feof($file))
{
echo '文件还未结束,再读一个字符:';
$ch = fgetc($file);
if(is_string($ch) && ereg('\w',$ch))
{
echo $ch."\r\n";
}
else
{
printf('0xX',$ch);
echo "\r\n";
}
echo '现在的文件位置是'.sprintf('0xX',ftell($file))."\r\n";
echo '文件'.(feof($file)?'已结束':'还未结束')."\r\n";
}
fclose($file);//解析器后面不再工作了,此时可以释放文件指针了。
$info = @$nil['value'][0]['info'];
if(!$info)
{
echo '这是一个有效的B-Encoding文件,但它不是一个有效的种子文件';
exit();
}
$name = $info['name.utf-8'] ?$info['name.utf-8']:$info['name'];
if(!$name)
{
echo '这是一个有效的B-Encoding文件,但它不是一个有效的种子文件';
exit();
}
echo $name."\r\n";
if($info['files'])
{
$index = 0;
foreach($info['files'] as $f)
{
$index += 1;
$path = $f['path.utf8'] ?$f['path.utf8'] :$f['path'];
if(!$path)
{
echo '文件列表中的第'.$index."个文件不含目录\r\n";
continue;
}
if(0 === strpos($path[0],"_____padding_file_"))continue;
$under_folder = false;
foreach($path as $item)
{
if($under_folder)
{
echo '/';
}else{
$under_folder = true;
}
echo $item;
}
echo "\r\n";
}
}
else
{
echo "仅有一个文件\r\n";
}
class Parser
{
private $_file;
public function __construct($file)
{
$this ->_file = $file;
}
public function __invoke($parent = array())
{
$ch = $this ->read();
switch($ch)
{
case 'i':
{
$n = $ch;
while(($ch = $this ->read()) != 'e')
{
if(!is_numeric($ch))
{
echo '在';
echo sprintf(
'0xX',ftell($this ->_file));
echo '解析数字时遇到错误',"\r\n";
echo '在i和e之间不应该出现非数字字符'."\r\n";
echo '意外的字符'.sprintf('0xX',$ch);
exit();
}
else
{
$n .= $ch;
}
}
$n += 0;
$offset = count($parent['value']);
$parent['value'][$offset] = $n;
return $parent;
}
break;
case 'd':
{
$node = array();
//这个$node变量作为字典对象准备加入到$parent的孩子节点中去
//$node['type'] = 'd';
while('e' != ($tmp = $this($node)))
{//每次给$node带来一个新孩子
$node = $tmp;
}
$child_count = count($node['value']);
if($child_count % 2 != 0)
{
echo '解析结尾于';
echo sprintf('0xX',ftell($this ->_file));
echo '的字典时遇到错误:'."\r\n";
echo '字典的对象映射不匹配';
exit();
}
$product = array();
for($i = 0; $i < $child_count; $i += 2)
{
$key = $node['value'][$i];
$value = $node['value'][$i + 1];
if(!is_string($key))
{
echo '无效的字典结尾于';
echo sprintf('0xX',ftell($this ->_file));
echo ":\r\n";
echo '解析[k => v]配对时遇到错误,k应为字符串';
exit();
}
$product[$key] = $value;
}
/*
* 思想是这样的:子节点想要加入父节点时,
* 往父节点的value数组添加。
* 当父节点收集好所需的信息后,
* 父节点自身再从它的value节点整合内容
* 对于字典和列表统一这样处理会大大降低代码量
*/
$offset = count($parent['value']);
$parent['value'][$offset] = $product;
return $parent;
}
break;
case 'l';
{
$node = array();
while('e' != ($tmp = $this($node)))
{
$node = $tmp;
}
$offset = count($parent['value']);
$parent['value'][$offset] = $node['value'];
return $parent;
}
break;
case 'e':
return 'e';
break;
default:
{
if(!is_numeric($ch))
{
$this ->unexpected_character(
ftell($this ->_file) - 1,$ch);
}
$n = $ch;
while(($ch = $this ->read()) != ':')
{
$n .= $ch;
if(!is_numeric($n))
{
unexpected_character(
ftell($this ->_file) - 1,$ch);
}
}
$n += 0;
$str = '';
for(; $n > 0; --$n)
{
$str .= $this ->read();
}
$offset = count($parent['value']);
$parent['value'][$offset] = $str;
return $parent;
}
break;
}
}
/*
* read函数包裹了$this ->_file变量
*/
function read()
{
if(!feof($this ->_file))
{
return fgetc($this ->_file);
}else{
echo '意外的文件结束';
exit();
}
}
/*
* unexpected_character函数接收2个参数
* 它用于指明脚本在何处遇到了哪个不合法的字符,
* 并在返回前终止脚本的运行。
*/
function unexpected_character($pos,$val)
{
$hex_pos = sprintf("0xX",$pos);
$hex_val = sprintf("0xX",$val);
echo 'Unexpected Character At Position ';
echo $hex_pos.' , Value '.$hex_val."\r\n";
echo "Analysing Process Teminated.";
exit();
}
}
?>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有