Hubro
Hubro

Reputation: 59323

How do I remove text from the console in PHP?

My goal is to print an updating 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?

Upvotes: 5

Views: 1096

Answers (4)

BlackHammer
BlackHammer

Reputation: 91

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();

Upvotes: 2

ceejayoz
ceejayoz

Reputation: 180004

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.

Upvotes: 0

Alex Howansky
Alex Howansky

Reputation: 53543

See Zend_ProgressBar

Upvotes: 0

Oswald
Oswald

Reputation: 31647

echo chr(8);

will print a backspace character.

Upvotes: 3

Related Questions