Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear PHP CLI output

Tags:

php

Try outputting a line of text and terminating it with "\r" instead of "\n".

The "\n" character is a line-feed which goes to the next line, but "\r" is just a return that sends the cursor back to position 0 on the same line.

So you can:

echo "1Done\r";
echo "2Done\r";
echo "3Done\r";

etc.

Make sure to output some spaces before the "\r" to clear the previous contents of the line.

[Edit] Optional: Interested in some history & background? Wikipedia has good articles on "\n" (line feed) and "\r" (carriage return)


I came across this while searching for a multi line solution to this problem. This is what I eventually came up with. You can use Ansi Escape commands. http://www.inwap.com/pdp10/ansicode.txt

<?php
function replaceOut($str)
{
    $numNewLines = substr_count($str, "\n");
    echo chr(27) . "[0G"; // Set cursor to first column
    echo $str;
    echo chr(27) . "[" . $numNewLines ."A"; // Set cursor up x lines
}

while (true) {
    replaceOut("First Ln\nTime: " . time() . "\nThird Ln");
    sleep(1);
}
?>

I recently wrote a function that will also keep track of the number of lines it last output, so you can feed it arbitrary string lengths, with newlines, and it will replace the last output with the current one.

With an array of strings:

$lines = array(
    'This is a pretty short line',
    'This line is slightly longer because it has more characters (i suck at lorem)',
    'This line is really long, but I an not going to type, I am just going to hit the keyboard... LJK gkjg gyu g uyguyg G jk GJHG jh gljg ljgLJg lgJLG ljgjlgLK Gljgljgljg lgLKJgkglkg lHGL KgglhG jh',
    "This line has newline characters\nAnd because of that\nWill span multiple lines without being too long",
    "one\nmore\nwith\nnewlines",
    'This line is really long, but I an not going to type, I am just going to hit the keyboard... LJK gkjg gyu g uyguyg G jk GJHG jh gljg ljgLJg lgJLG ljgjlgLK Gljgljgljg lgLKJgkglkg lHGL KgglhG jh',
    "This line has newline characters\nAnd because of that\nWill span multiple lines without being too long",
    'This is a pretty short line',
);

One can use the following function:

function replaceable_echo($message, $force_clear_lines = NULL) {
    static $last_lines = 0;

    if(!is_null($force_clear_lines)) {
        $last_lines = $force_clear_lines;
    }

    $term_width = exec('tput cols', $toss, $status);
    if($status) {
        $term_width = 64; // Arbitrary fall-back term width.
    }

    $line_count = 0;
    foreach(explode("\n", $message) as $line) {
        $line_count += count(str_split($line, $term_width));
    }

    // Erasure MAGIC: Clear as many lines as the last output had.
    for($i = 0; $i < $last_lines; $i++) {
        // Return to the beginning of the line
        echo "\r";
        // Erase to the end of the line
        echo "\033[K";
        // Move cursor Up a line
        echo "\033[1A";
        // Return to the beginning of the line
        echo "\r";
        // Erase to the end of the line
        echo "\033[K";
        // Return to the beginning of the line
        echo "\r";
        // Can be consolodated into
        // echo "\r\033[K\033[1A\r\033[K\r";
    }

    $last_lines = $line_count;

    echo $message."\n";
}

In a loop:

foreach($lines as $line) {
    replaceable_echo($line);
    sleep(1);
}

And all lines replace each other.

The name of the function could use some work, just whipped it up, but the idea is sound. Feed it an (int) as the second param and it will replace that many lines above instead. This would be useful if you were printing after other output, and you didn't want to replace the wrong number of lines (or any, give it 0).

Dunno, seemed like a good solution to me.

I make sure to echo the ending newline so that it allows the user to still use echo/print_r without killing the line (use the override to not delete such outputs), and the command prompt will come back in the correct place.


i know the question isn't strictly about how to clear a SINGLE LINE in PHP, but this is the top google result for "clear line cli php", so here is how to clear a single line:

function clearLine()
{
    echo "\033[2K\r";
}

function clearTerminal () {
  DIRECTORY_SEPARATOR === '\\' ? popen('cls', 'w') : exec('clear');
}

Tested on Win 7 PHP 7. Solution for Linux should work, according to other users reports.


something like this :

for ($i = 0; $i <= 100; $i++) {
    echo "Loading... {$i}%\r";
    usleep(10000);
}

Use this command for clear cli:

echo chr(27).chr(91).'H'.chr(27).chr(91).'J';   //^[H^[J