[英]PHP replace last x character in console
I want to replace last x character from console output. 我想从控制台输出中替换最后的x字符。 I can remove complete line by using
\\r
. 我可以使用
\\r
删除完整的行。 But is there a way to just remove last x character from console output ? 但有没有办法从控制台输出中删除最后一个x字符?
In PHP : 在PHP中 :
To print a backspace character, use 0x08
. 要打印退格符,请使用
0x08
。 BUT, there's a problem. 但是,有一个问题。 If you follow that by a newline
"\\n"
(which you normally would) then the cursor is moved back to EOL, and the characters are exposed again. 如果你按照换行符
"\\n"
(你通常会这样做),那么光标将移回EOL,然后再次显示这些字符。 Good news is that if you follow the 0x08
with the same number of spaces, it works fine. 好消息是,如果您使用相同数量的空格跟随
0x08
,它可以正常工作。 For example, to remove the last two characters: 例如,要删除最后两个字符:
<?php
print "Hello";
printf("%c%c",0x08,0x08);
print " \n";
?>
Which prints Hel
. 哪个打印
Hel
。 Note the use of printf()
with %c
to use an integer (0x08) as a character. 注意使用带有
%c
的printf()
来使用整数(0x08)作为字符。
For a generic way to remove x number of characters: 对于删除x个字符的通用方法:
<?php
$x = 4;
$str = "Those who are about to die salute you";
$hacky = str_repeat(chr(0x08),$x).str_repeat(" ",$x);
print "$str$hacky\n";
?>
which prints Those who are about to die salute
. 打印
Those who are about to die salute
。 Note the use of chr(0x08)
here, which returns the character at position 8 in the character set. 注意这里使用
chr(0x08)
,它返回字符集中位置8的字符。
您可以使用cut
命令。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.