Clear PHP CLI output
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)
How can clear screen in php cli (like cls command)
For Windows users :
system('cls');
For Linux users :
system('clear');
Clear CMD-shell with php
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.
Update Command-line Output, i.e. for Progress
This can be done using ANSI Escape Sequences -- see here for a list.
In PHP, you'll use "\033"
when it's indicated ESC
on that page.
In your case, you could use something like this :
echo "Progress : "; // 5 characters of padding at the end
for ($i=0 ; $i<=100 ; $i++) {
echo "\033[5D"; // Move 5 characters backward
echo str_pad($i, 3, ' ', STR_PAD_LEFT) . " %"; // Output is always 5 characters long
sleep(1); // wait for a while, so we see the animation
}
I simplified a bit, making sure I always have 5 extra characters, and always displaying the same amount of data, to always move backwards by the same number of chars...
But, of course, you should be able to do much more complicated, if needed ;-)
And there are many other interesting escape sequences : colors, for instance, can enhance your output quite a bit ;-)
Php Command Line Output on the fly
First, the Windows-style end-of-line marker is \r\n
, not \n\r
. Not many systems ever used \n\r
, but they are rare enough that you can forget about them now.
Second, chances are good the output is being block buffered -- you can either use ob_implicit_flush(1)
to automatically insert a flush()
command after every output command. Or, you could call flush()
manually, when you need to flush output.
How do I remove text from the console in PHP?
echo chr(8);
will print a backspace character.
php command line change text output
use \r
instead of \n
. \r
is a carriage return, it will jump back to the beginning of the line without a newline.
$percent = 0;
for ($i = 0; $i <= 100; $i++) {
echo $percent . "\r";
sleep(1);
$percent++;
}
This is working on Windows, Linux and MacOS
Related Topics
How to Count Query Result Rows
Laravel 4: How to "Order By" Using Eloquent Orm
PHP Errors Not Being Displayed in the Browser [Ubuntu 10.10]
Is It Recommended to Store PHP Sessions in Memcache
Differencebetween Site_Url() and Base_Url()
Algorithms for String Similarities (Better Than Levenshtein, and Similar_Text)? PHP, Js
How to Call a Controller Function Inside a View in Laravel 5
Passing Multiple PHP Variables to Shell_Exec()
How to Re-Format Datetime String in PHP
Count and Limit the Number of Files Uploaded (HTML File Input)
Can't Return a Result Set in the Given Context
Laravel 5.2 Not Reading Env File
How to Store an Array into MySQL
Save PHP Variables to a Text File
Pdo Fetchall Group Key-Value Pairs into Assoc Array
Mysql: Add Sequence Column Based on Another Field