从命令行捕获进度 [英] Capture Progress From Command Line

查看:194
本文介绍了从命令行捕获进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

%总计%接收%Xferd平均速度时间时间时间当前
载入上传总剩余速度
100 12.4M 100 12.4M 0 0 4489k 0 0:00:02 0:00:02 - - : - : - 4653k



以上是下载文件时从命令行输出的CURL。我已经捕获这使用PHP这样,但我有麻烦工作如何使用pre_match提取百分比完成。

  $ handle = popen('curl -o'.VIDEOPATH。$ fileName。'。flv'。$ url,'rb'); 

while(!feof($ handle))
{
$ progress = fread($ handle,8192);
//我甚至不知道我在尝试什么
$ pattern ='/(?<Total> ;[0-9]{1,3}\.[0-9] {1,2})%的(α
// //总共接收到的总数,不管怎样,然后乘以100
if(preg_match_all($ pattern,$ progress,$ matches)){
fwrite($ fh,$ matches [0] [0 ]。\r\\\
);
}

}

我该如何做?



感谢



更新

h2>

感谢ylebre的帮助。

  $ handle = popen('curl -o'.VIDEOPATH。$ fileName。'。flv' 。$ url。'2& 1','rb'); //确保它保存到视频

while(!feof($ handle))
{

$ line = fgets($ handle,4096); //从输入句柄中获取一行
echo'< br> Line'。$ line。'< br>';
$ line = preg_replace(/ s + /,,$ line); //用一个
替换双重空格$ fields = explode(,$ line); //将空格上的输入拆分成字段array
echo'< br> Fields:'。$ fields [0];
fwrite($ fh,$ fields [0]); //将fields数组的一部分写入输出文件

}

我得到这个输出到浏览器:






行%总%接收%Xferd平均速度时间时间时间当前

字段:
行载入上传总剩余速度



字段:
行0 1340k 0 4014 0 0 27342 0 0:00:50 - : - : - 0:00:50 27342 41 1340k 41 552k 0 0 849k 0 0:00:01 - : - :0:00: 01 1088k 100 1340k 100 1340k 0 0 1445k 0 - : - : - : - : - - : - : - 1711k



字段:






如何提取百分比部分?也许CURL本身可以做到这一点 - 你会问这个问题。

解决方案

显示的进度可能是更新



我推荐的下一步是获取一行输入,并尝试得到正则表达式工作。



如果我正确读取输出,您也可以在空格处拆分字符串。如果你开始通过将所有的双空格替换成一个。之后你可以使用explode()获取一个数组,你可以使用print_r来查看里面的内容。



/ p>

  $ line = fgets($ handle,4096); //从输入句柄中获取一行
$ line = preg_replace(/ s + /,,$ line); //用一个
替换双重空格$ fields = explode(,$ line); //将空格的输入拆分成字段array
fwrite($ fh,$ fields [0]); //将fields数组的一部分写入输出文件

只要字段中的顺序



希望这有助于!


% Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 12.4M 100 12.4M 0 0 4489k 0 0:00:02 0:00:02 --:--:-- 4653k

The above is a CURL output from the command line when download the file. I have captured this using PHP like so, but I am having trouble working out how to use pre_match to extract the percentage done.

$handle = popen('curl -o '.VIDEOPATH.$fileName.'.flv '.$url, 'rb');

while(!feof($handle))
{
    $progress = fread($handle, 8192);
    //I don't even know what I was attempting here
    $pattern = '/(?<Total>[0-9]{1,3}\.[0-9]{1,2})% of (?<Total>.+) at/';
    //divide received by total somehow, then times 100
    if(preg_match_all($pattern, $progress, $matches)){
    fwrite($fh, $matches[0][0]."\r\n");
    }

} 

How can I do this? Please note, I have no idea what I am doing with the above preg_match_all!

Thanks

Update

Thanks to the help of ylebre. I have this so far.

$handle = popen('curl -o '.VIDEOPATH.$fileName.'.flv '.$url.' 2>&1', 'rb');//make sure its saved to videos

while(!feof($handle))
{

    $line = fgets($handle, 4096); // Get a line from the input handle
    echo '<br>Line'.$line.'<br>';
    $line = preg_replace("/s+/", " ", $line); // replace the double spaces with one
    $fields = explode(" ", $line); // split the input on spaces into fields array
    echo '<br>Fields: '.$fields[0];
    fwrite($fh, $fields[0]); // write a part of the fields array to the output file

} 

I get this output to the browser:


Line % Total % Received % Xferd Average Speed Time Time Time Current

Fields: Line Dload Upload Total Spent Left Speed

Fields: Line 0 1340k 0 4014 0 0 27342 0 0:00:50 --:--:-- 0:00:50 27342 41 1340k 41 552k 0 0 849k 0 0:00:01 --:--:-- 0:00:01 1088k 100 1340k 100 1340k 0 0 1445k 0 --:--:-- --:--:-- --:--:-- 1711k

Fields: Line


How do I extract the percentage part only? Maybe CURL can do this by itself - hmm will ask a question on this.

解决方案

The progress that is showing up is probably updating the information in the same spot, so it will help if you know what you are parsing exactly.

The next step I recommend is taking one line of input, and trying to get the regexp to work on that.

You could also just split the string at the spaces if I'm reading the output correctly. If you start out by replacing all the double spaces into one. After that you can use explode() to get an array with the values, which you can print_r to take a peek what is inside.

This would be something like:

$line = fgets($handle, 4096); // Get a line from the input handle
$line = preg_replace("/s+/", " ", $line); // replace the double spaces with one
$fields = explode(" ", $line); // split the input on spaces into fields array
fwrite($fh, $fields[0]); // write a part of the fields array to the output file

As long as the ordering in the fields remains the same, your resulting array should give you a consistent result.

Hope this helps!

这篇关于从命令行捕获进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆