bash 如何从 php cli 回显退格?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15157666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 04:42:51  来源:igfitidea点击:

How do I echo a backspace from php cli?

phpbashcommand-line-interface

提问by Benubird

This question: https://askubuntu.com/questions/16149/overwrite-previous-output-in-bash-instead-of-appending-it

这个问题:https: //askubuntu.com/questions/16149/overwrite-previous-output-in-bash-instead-of-appending-it

Explains how to do a countdown as a bash script. I want to do the same thing, but I need to do it in PHP. Is there a way to echo a backspace?

解释如何以 bash 脚本的形式进行倒计时。我想做同样的事情,但我需要用 PHP 来做。有没有办法回显退格?

e.g.

例如

echo "Counting down 60\n";
sleep(1);
echo "\b\b\b59\n";
sleep(1);
echo "\b\b\b58\n";

But, echo "\b" doesn't do anything.

但是, echo "\b" 不做任何事情。

回答by Olaf Dietsche

From Strings - Double quoted, there is no \brecognized as an escape sequence. You can use the ASCIIbackspace hex or octal code

字符串 - 双引号,没有被\b识别为转义序列。您可以使用ASCII退格十六进制或八进制代码

$bs = "\x08";
echo "Counting down 60";
sleep(1);
echo "$bs$bs59";
sleep(1);
echo "$bs$bs58";

or in a loop all the way down to zero

或一直循环到零

$bs = "\x08";
for ($i = 59; $i >= 0; --$i) {
    sleep(1);
    printf("$bs$bs%2d", $i);
}

You must omit the newline \nas well.

您还必须省略换行符\n

回答by Liv

The backspace character corresponds to the ASCII character code 8, so you can output it using PHP's char()function.

退格符对应的是ASCII字符码8,所以可以用PHP的char()函数输出。

Ex. if you do something like:

前任。如果你做这样的事情:

echo 'Hello, world!' . chr(8);

The resulting output is:

结果输出是:

Hello, world