My goal is to print an updatin开发者_JAVA百科g progress percentage to the console (in both Linux and Windows). Currently I just print out the percentage each 10%, but I would prefer that it updated itself every 1% without filling the screen with percentages.
Is it possible to remove text you have written to the console in PHP?
echo chr(8);
will print a backspace character.
very simple Note the example below
$removeLine = function (int $count = 1) {
foreach (range(1,$count) as $value){
echo "\r\x1b[K"; // remove this line
echo "\033[1A\033[K"; // cursor back
}
};
echo "-----------------------------\n";
echo "--------- Start -----------\n";
echo "-----------------------------\n";
sleep(2);
$removeLine(3);
echo 'hi';
sleep(2);
$removeLine();
echo 'how are you ?';
die();
See Zend_ProgressBar
PEAR's Console_ProgressBar is useful for this sort of use case.
To clear the console entirely, you can use:
if($_SERVER['SHELL']) {
print chr(27) . "[H" . chr(27) . "[2J";
}
which is quite a bit easier than keeping track of how many characters to backspace.
精彩评论