简体   繁体   English

PHP CLI,Windows 7 CMD和CLS / FF需要相处。 (从CLI清除Win7 CMD和PHP)

[英]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). 我正在用PHP编写终端应用程序,以便在Windows 7(x64 Ultimate)的花哨命令提示符下运行PHP CLI。 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. CMD有一个奇特的CLS命令就是这么做的。 So, when I run the PHP line system('cls'); 所以,当我运行PHP行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. 或执行清除屏幕命令的任何变化(包括说使用system("command /C cls");一个答案system("command /C cls");这不起作用)终端输出金星符号。 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? 为什么PHP CLI认为显示CMD女孩会清除屏幕,我怎样才能让CMD 真正清除? 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: 经过多次搜索,我终于找到了一个适用于Windows 7的依赖于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 . 这在我的标准控制台中适用,因为我安装了Ansicon ...如果您遇到问题,可能需要尝试使用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). 如果您正在向公众分发CLI,那么这个解决方案不够好,但对于内部使用它可能是可接受的(即,如果您可以在所有机器上运行ANSI)。

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. 这是一个有点旧的ASCII命令的skool方法,它仍然适用于Windows DOS(大多数情况下)......我用它来改变输出的颜色。

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 :) 如果您为转义键“\\ 033”的八进制引用,则DOS转义字符“[”和一个可以执行许多不同任务的键代码,例如更改颜色和...清除屏幕:)

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). 使用以下命令清除php中的屏幕作为echo语句(我没有设法让system()使用此命令,但echo对我来说已经足够了)。

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 进一步阅读: 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): 找到一种简单的方法,如果你从批处理文件运行PHP(我在Win7 x64终极上使用XAMPP):

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. 这个批处理文件在运行PHP之前会清除屏幕,甚至不会再显示命令promt,因为行的开头是@。 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. 你也可以使用\\ r来到一行的开头,所以如果你只需要重绘一行 (例如一个进度指示器),这可以是一个解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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