EdwardBlack EdwardBlack - 7 months ago 18
Bash Question

Clear CMD-shell with php

I have this simple php script which outputs a string every second.

<?php
$i = 1;

while(1)
{
exec("cls"); //<- Does not work
echo "test_".$i."\n";

sleep(1);
$i++;
}


I execute the script in the command shell on windows (
php myscript.php
) and try to clear the command shell before every cycle. But I don't get it to work. Any ideas?

Answer

How about this?

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

the str_repeat() function executes outside of the while loop, and instead of ending each echo with a new line, it moves the pointer back to the existing line, and writes over the top of it.

Comments