Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Seeking STDOUT in PHP

I have a php script that is running in CLI and I want to display the current percent progress so I was wondering if it is possible to update the STDOUT to display the new percent.

When I use rewind() or fseek() it just throws an error message.

like image 636
instigator Avatar asked Mar 05 '10 14:03

instigator


1 Answers

See this code:

<?php
echo "1";
echo chr(8);
echo "2";

The output is only 2 since "chr(8)" is the char for "backspace".

So just print the amount of chars you need to go back and print the new percentage.

Printing "\r" works too on Linux and Windows but isn't going to cut it on a mac

Working example:

echo "Done: ";
$string = "";
for($i = 0; $i < 100; ++$i) {
    echo str_repeat(chr(8), strlen($string));
    $string = $i."%";
    echo $string;
    sleep(1);
}
like image 112
edorian Avatar answered Oct 04 '22 04:10

edorian