Is there any alternative to system ('cls') in PHP Console?

12

I'm using PHP (5.7) on the console (Windows 10), but I'm not able to clear the screen. I give system('cls') and only appears a square with a question mark, without cleaning the screen.

I'vedoneitthisway:

for($i=0;$i<50;$i++)echo"\r\n";

Clean, but the next message is down there on the console and it takes a while to clean, in fact it just skips the lines, and I did not want that to happen. system('cls') actually clears the console.

Is there an alternative or a way to solve this problem?

    
asked by anonymous 26.12.2016 / 23:14

4 answers

2

The codes for all other responses did not work in my environment:
Windows 10, PHP7 and earlier versions up to version 5.3.6.

Of all the answers found on the internet, the most bizarre is to do dozens of line breaks. It's like sweeping dirt under the rug.

In summary, at the end of the tests, I concluded that under Windows 10, regardless of the version of PHP, just print "\r" in quotes. Other commands used with the chr() function or writing escape characters from the CMD seem to have no effect at all.

Test with the chr ()

<?php
echo time()."\r";
chr(27);
sleep(1);
echo time();

I put sleep(1) to test. It is not necessary to be there.

It is important that at the end of each printed line you have a return \r in double quotation marks.

The original script was adapted from this response: link

In that same response, it was important to comment on @ recursion.ninja since the original code uses \n , which did not work. However, after switching to \r it worked perfectly.

I also reduced all the code to simplify and clarify where the "magic" is. I removed the echo constructor because it does not make sense to use. Using echo causes those foreign symbols to be printed.

Whenrunning,1484622554wasdisplayed.Itwasthenreplacedby1484622555andlast,1484622556.

That^ZisduetoENTER,CTRL+Z,ENTERinordertoexecutethescript.

DependingontheversionofWindows,itisCTRL+D.

Fromthislogic,itispossibletoleavemoredynamiccreatingakindofwraperforthescriptstobeexecuted.Anexampleofawraperisintheoriginalresponse.Youjustneedtoremoveunnecessarystitchesandmakesomeadjustments.Whicharedescribedhere.

Curiosities:

Outofcuriosity,Itestedifit'sreallythechr()functionthatdoesthelinecleanup.Imodifiedthevalueoftheparametertoanyothervalueandgavethesameresult.ThenItriedremovingthefunctionandtomysurprise,itworkedasexpected.

<?phpechotime()."a\r";
//chr(0);
//sleep(1);
echo time()."b\r";
//chr(0);
//sleep(1);
echo time()."c";

In this test, it seems like chr() does not make any difference. You printed the last value concatenated with "c" without printing the previous two.

If you delimit \r with single quotation mark, print the three concatenated lines.     

With this test we can see that it is enough to only print "\r" in double quotation marks.

At least this works in Windows 10.

Common mistakes in other responses mean that virtually everyone prints the output of a constructor. This does not make sense and causes the impression of strange symbols like this .

Invoking cmd /c cls by the exec() function also does not make sense since cls will be executed in another instance of CMD . Instance that is empty and not visually accessible, that is, it is cleaning the invisible void. rsrs

PHP Libraries

In PHP there is a specific function to perform the function of clear/cls . The function is ncurses_clear () .

This function belongs to the library NCurses and is available for Linux systems. However it is an experimental library and the last build is from 2012. Typically one does not trust libraries of this type and the PHP manual itself warns. But nothing prevents one from continuing. Just read the source code, refine and compile.

    
17.01.2017 / 04:40
9

Try the following:

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

Source: PHP clear terminal screen

    
29.12.2016 / 11:50
5

cls is not a Windows executable, but a command interpreter command ( cmd.exe in Windows 7, ...).

So, to be able to execute, you should invoke the command interpreter:

cmd.exe /c cls

The argument /c forces the interpreter to close after executing the command.

    
08.01.2017 / 00:41
3

You can try this:

<?php
echo "Mensagem antes de limpar\n";

echo "3[2J3[1;1H";

echo "Mensagem depois de limpar\n";
?>

Where ANSI does not actually clean the screen, but its 3[2J complement resets to the start of the 'next screen', you can only test with 3[1;1H , but you'll see that the text printed after this 3[2J will be slightly below the start of the prompt window.

This solution also works on the Linux terminal, only on the Power Shell that I tested and did not work.

The result will be this:

    
02.01.2017 / 14:20