如何在 php cli 中清除屏幕(如 cls 命令)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24327544/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:17:00  来源:igfitidea点击:

How can clear screen in php cli (like cls command)

phpwindows

提问by Root

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

当 PHP 脚本从命令行(Windows)运行时,如何从脚本中清除控制台屏幕。

for example :

例如 :

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

    // below command execute to clear the console windows
    **COMMAND**
}

采纳答案by Olivier

For Windows users :

对于 Windows 用户:

system('cls');

For Linux users :

对于 Linux 用户:

system('clear');

回答by M. Chris

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

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

回答by trejder

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

找到了一个解决方案,它适用于cmdGitBash 和 GitBash。然而,这是我能想到的最丑陋的清除控制台屏幕的实现。遗憾的是,没有任何可行的替代方案。

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.

这是2006 年这篇非工作(对我而言)帖子的修改(固定?)版本。

回答by Robin Rijkeboer

You can do this by using:

您可以使用以下方法执行此操作:

ncurses_clear();

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

来源: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.

编辑:正如 trejder 所说,此解决方案仅适用于受支持的平台,似乎 windows 不是其中之一。