使用php清除CMD shell


Clear CMD-shell with php

我有一个简单的php脚本,它每秒输出一个字符串。

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

我在windows上的命令shell(php myscript.php)中执行脚本,并尝试在每个周期之前清除命令shell。但我不能让它发挥作用。有什么想法吗?

这个怎么样?

<?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++;
}

str_repeat()函数在while循环之外执行,它没有用新行结束每个回波,而是将指针移回现有行,并在其顶部进行写入。

你能检查这个解决方案吗

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

显然,您必须存储变量的输出,然后print它才能成功清除屏幕:

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

总之:

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

我在Linux上用clear而不是cls(等效命令)测试了它,它运行得很好。

的重复➝这个问题

在Windows下,没有这样的东西

@exec('cls');

对不起!你所能做的就是像这里一样寻找一个可执行文件(而不是cmd内置命令)。。。

您必须将输出打印到终端:

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

如果是linux服务器,请使用以下命令(clear)如果是窗口服务器,请使用cls我希望它能在上运行

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

第二溶液

<?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++;
}

这个对我有效,也经过了测试。