title: Capture Progress From Command Line
body: <p>% 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</p>
  
  <p>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.</p>
  
  <pre><code>$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 = '/(?&lt;Total&gt;[0-9]{1,3}\.[0-9]{1,2})% of (?&lt;Total&gt;.+) at/';
      //divide received by total somehow, then times 100
      if(preg_match_all($pattern, $progress, $matches)){
      fwrite($fh, $matches[0][0]."\r\n");
      }
  
  } 
  </code></pre>
  
  <p>How can I do this? Please note, I have no idea what I am doing with the above preg_match_all!</p>
  
  <p>Thanks</p>
  
  <h2>Update</h2>
  
  <p>Thanks to the help of ylebre. I have this so far.</p>
  
  <pre><code>$handle = popen('curl -o '.VIDEOPATH.$fileName.'.flv '.$url.' 2&gt;&amp;1', 'rb');//make sure its saved to videos
  
  while(!feof($handle))
  {
  
      $line = fgets($handle, 4096); // Get a line from the input handle
      echo '&lt;br&gt;Line'.$line.'&lt;br&gt;';
      $line = preg_replace("/s+/", " ", $line); // replace the double spaces with one
      $fields = explode(" ", $line); // split the input on spaces into fields array
      echo '&lt;br&gt;Fields: '.$fields[0];
      fwrite($fh, $fields[0]); // write a part of the fields array to the output file
  
  } 
  </code></pre>
  
  <p>I get this output to the browser:</p>
  
  <hr>
  
  <p>Line % Total % Received % Xferd Average Speed Time Time Time Current</p>
  
  <p>Fields:
  Line Dload Upload Total Spent Left Speed</p>
  
  <p>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</p>
  
  <p>Fields:
  Line</p>
  
  <hr>
  
  <p>How do I extract the percentage part only? Maybe CURL can do this by itself - hmm will ask a question on this.</p>