41 php
我正在尝试在我的php CLI应用程序上运行一个"实时"进度指示器.而不是输出为
1Done
2Done
3Done
Run Code Online (Sandbox Code Playgroud)
我希望它清除并显示最新结果.system("command\C CLS")不起作用.也没有ob_flush(),flush()或我发现的任何其他内容.
我正在运行Windows 7 64位终极版,我注意到命令行实时输出,这是出乎意料的.每个人都警告我,不会...但它确实... 64位额外奖励?
干杯求救!
如果可以的话,我想避免回复24条新线路.
dka*_*ins 100
尝试输出一行文本并以"\ r"而不是"\n"结束.
"\n"字符是一个换行符,它转到下一行,但"\ r \n"只是一个返回,它将光标发送回同一行的位置0.
所以你可以:
echo "1Done\r";
echo "2Done\r";
echo "3Done\r";
Run Code Online (Sandbox Code Playgroud)
等等
确保在"\ r"之前输出一些空格以清除该行的先前内容.
[编辑]可选:对某些历史和背景感兴趣?维基百科有关于"\n"(换行)和"\ r"(回车)的好文章
nol*_*pro 32
我在寻找这个问题的多线解决方案时遇到了这个问题.这就是我最终提出的.您可以使用Ansi Escape命令.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);
}
?>
Run Code Online (Sandbox Code Playgroud)
我最近写了一个函数,它也会跟踪它最后输出的行数,因此你可以用换行符给它任意的字符串长度,它将用当前的一行替换最后一个输出.
使用字符串数组:
$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',
);
Run Code Online (Sandbox Code Playgroud)
可以使用以下功能:
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";
}
Run Code Online (Sandbox Code Playgroud)
在循环中:
foreach($lines as $line) {
replaceable_echo($line);
sleep(1);
}
Run Code Online (Sandbox Code Playgroud)
并且所有线路互相替换.
函数的名称可以使用一些工作,只是鞭打它,但这个想法是合理的.将它作为第二个参数输入(int),它将替换上面的那么多行.如果您在其他输出之后打印,并且您不想替换错误的行数(或任何,将其赋予0),这将非常有用.
Dunno,对我来说似乎是一个很好的解决方案.
我确保回显结束换行符,以便它允许用户仍然使用echo/ print_r不杀死该行(使用覆盖不删除此类输出),并且命令提示符将返回到正确的位置.
function clearTerminal () {
DIRECTORY_SEPARATOR === '\\' ? popen('cls', 'w') : exec('clear');
}
Run Code Online (Sandbox Code Playgroud)
根据其他用户的报告,在Win 7 PHP 7上进行了测试。适用于Linux的解决方案应该可以工作。
这样的事情:
for ($i = 0; $i <= 100; $i++) {
echo "Loading... {$i}%\r";
usleep(10000);
}
Run Code Online (Sandbox Code Playgroud)
我知道问题不仅仅在于如何在PHP中清除单行,但这是“ clear line cli php”的google最高搜索结果,因此这是如何清除单行:
function clearLine()
{
echo "\033[2K\r";
}
Run Code Online (Sandbox Code Playgroud)