简体   繁体   中英

PHP CLI, Windows 7 CMD, and CLS/FF need to get along. (Clear Win7 CMD with PHP from CLI)

Alright now, I have been searching everywhere for a way to accomplish this and apparently nobody on the internet knows what's up. And by internet I mean Google's scope of it.

I am writing a terminal application in PHP to run in the PHP CLI in the fancy command prompt of Windows 7 (x64 Ultimate). In the program, I want to clear the screen. The data displayed on the screen can be any amount of characters spanning any amount of lines. I don't want any backspace character hacks or instructions on using carriage returns. I want to clear the screen.

CMD has a fancy CLS command that does just that. So, when I run the PHP line system('cls'); or any variation of executing the clear screen command (Including that one answer that said to use system("command /C cls"); which did not work) the terminal outputs the venus symbol. The girl sign, the "upside down holy hand grenade", whatever you wish to call it. As it turns out, this is apparently some way to show the linefeed character (which if the terminal actually behaved as it should, would clear the screen.)

Why does the PHP CLI think that showing CMD a girl will make it clear the screen, and how can I get CMD to actually clear? I've tried sending it a linefeed character, sending it a girl symbol, sending it chips and cookies and all forms of nice things. Nothing worked. So the screen shall forever remain unclear, which you can imagine can be annoying when you have to re-draw something. Anyway, your help is welcome.

This is the only, yet very ugly, way of doing this, that I found working so far:

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

After much searching, I finally found an answer that works for me in Windows 7 that relies on ANSI:

echo chr(27)."[H".chr(27)."[2J";

This works for me in the standard console because I installed Ansicon ... if you're having trouble you might want to try Console2 .

If you're distributing a CLI to the public then this solution is not good enough, but for in-house use it might be acceptable (ie, if you can get ANSI going on all the machines).

After trying to find a solution to this myself and spending nearly an hour with no luck, the answer was right there in front of me...

It's a bit old skool method of ASCII commands which still work on Windows DOS today (in most cases)... I was using it to change the colour of the output.

If you the octal reference for escape key "\\033" then the DOS escape character "[" and a key code which can do lots of different tasks such as changing colour and... clearing the screen :)

Use the following to clear the screen in php as an echo statement (I haven't managed to get system() to work with this command however echo is enough for me).

echo "\033[2J"; //clears the screen
echo "\033[1;1H"; //resets pointer back to 1,1 (top, left)

I really hope this has helped someone :)

Further reading: http://www.robvanderwoude.com/ansi.php

Cheers Dal

Found a simple way, if you run the PHP from a batch file (I'm using XAMPP on Win7 x64 ultimate):

cls
@c:\xampp\php\php program_to_run.php

This batch file clears the screen before running PHP, and even does not display the command promt again beacause of the @ on the beginning of the line. Result: starting with clear console screen!

Also you can use \\r to get to the beginning of a line, so if you only need to redraw one line (for example a progress indicator), this can be a solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM