25

When PHP script run from command line (windows) , how can clear the console screen from script .

for example :

while(true){
    // sleep for 10 seconds , then clear the console
    sleep(10);

    // below command execute to clear the console windows
    **COMMAND**
}
Root
  • 1,987
  • 5
  • 26
  • 57

4 Answers4

48

If you did not have any luck with the solutions above, consider the following

echo chr(27).chr(91).'H'.chr(27).chr(91).'J';   //^[H^[J  

Hope it will help.

Source : http://pank.org/blog/2011/02/php-clear-terminal-screen.html

M. Chris
  • 782
  • 1
  • 5
  • 5
17

For Windows users :

system('cls');

For Linux users :

system('clear');
checksum
  • 6,067
  • 4
  • 34
  • 33
OlivierLarue
  • 2,203
  • 20
  • 26
5

Found a solution, that works in both cmd and GitBash. However, this is the ugliest implementation of clearing console-screen I can think of. Pity, that there isn't any working alternative.

The "magic" is to... poke console with fifty new-lines, like that:

public function clearStdin()
{
    for ($i = 0; $i < 50; $i++) echo "\r\n";
}

This is a modified (fixed?) version of this non-working (for me) post from 2006.

trejder
  • 16,472
  • 26
  • 116
  • 210
0

You can do this by using:

ncurses_clear();

Source: http://www.php.net/manual/en/function.ncurses-clear.php

Edit: As trejder says this solution is only for supported platforms, it seems windows is not one of them.

Lost F.
  • 420
  • 3
  • 15