Clear CMD shell with php

I have this simple php script that prints a line every second.

<?php $i = 1; while(1) { exec("cls"); //<- Does not work echo "test_".$i."\n"; sleep(1); $i++; } 

I run a script in a command shell in windows ( php myscript.php ) and try to clear the shell of commands before each loop. But I do not work. Any ideas?

+5
source share
6 answers

How about this?

 <?php $i = 1; echo str_repeat("\n", 300); // Clears buffer history, only executes once while(1) { echo "test_".$i."\r"; // Now uses carriage return instead of new line sleep(1); $i++; } 

the str_repeat() function is executed outside the while , and instead of ending each echo with a new line, it moves the pointer back to the existing line and writes on top of it.

+3
source

You can check this solution.

 $i = 1; echo PHP_OS; while(1) { if(PHP_OS=="Linux") { system('clear'); } else system('cls'); echo "test_".$i."\n"; sleep(1); $i++; } 
+2
source

Apparently you need to save the output of the variable and then print so that it successfully clears the screen:

 $clear = exec("cls"); print($clear); 

Together:

 <?php $i = 1; while(1) { $clear = exec("cls"); print($clear); echo "test_".$i."\n"; sleep(1); $i++; } 

I tested it on Linux using clear instead of cls (equivalent command) and it worked fine.

+1
source

Duplicate โž this question

On Windows there is no such thing as

 @exec('cls'); 

Excuse me! All you could do was search for the executable (and not the cmd built-in) as here ...

+1
source

You should print the output to the terminal:

 <?php $i = 1; while(1) { exec("cls", $clearOutput); foreach($clearOutput as $cleanLine) { echo $cleanLine; } echo "test_".$i."\n"; sleep(1); $i++; } 
0
source

if linux server uses the following command ( clear ) if this is using cls window server i hope this works

 $i = 1; while(1) { exec("clear"); //<- This will work echo "test_".$i."\n"; sleep(1); $i++; } 

second decision

 <?php $i = 1; echo PHP_OS; while(1) { if(PHP_OS=="Linux") { $clear = exec("clear"); print($clear); } else exec("cls"); echo "test_".$i."\n"; sleep(1); $i++; } 

This one also worked for me, it was also tested.

0
source

Source: https://habr.com/ru/post/1246934/


All Articles