开发者

How do I remove text from the console in PHP?

开发者 https://www.devze.com 2023-03-07 11:28 出处:网络
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 it

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号