php 输出期间的PHP“退格”字符可能吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1440011/
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-08-25 02:35:15  来源:igfitidea点击:

PHP "backspace" character during output possible?

php

提问by Jeff

I have a feeling the answer is "it's not possible," but thought I'd ask to satisfy my curiosity.

我有一种感觉,答案是“这是不可能的”,但我想我会要求满足我的好奇心。

I have some code that's echoed where the \n is unavoidable:

我有一些代码在 \n 不可避免的地方得到了回应:

echo "Hello \n";
echo "World!";

I'd like the line to simply read (in the code output):

我希望该行可以简单地读取(在代码输出中):

Hello World!

你好,世界!

... thus removing the \n.

...从而删除\n。

So I was wondering if it's possible to execute a "backspace" character during PHP's output?

所以我想知道是否可以在 PHP 输出期间执行“退格”字符?

Something simple like str_replace( "\n", 'backspace-character', $str );

一些简单的像 str_replace( "\n", 'backspace-character', $str );

回答by Brenton Alker

Yes, the backspace character is ASCII character code 8 (According to the ASCII table), so you can output it in php using chr(). eg:

是的,退格字符是 ASCII 字符代码 8(根据ASCII 表),因此您可以使用chr()在 php 中输出它。例如:

echo 'ab' . chr(8);

will output "a"

将输出“a”

回答by Peter Bailey

If the output target is HTML then extra spaces don't matter - browsers don't render multiple, contiguous spaces (which is why we have  )

如果输出目标是 HTML,则额外的空格无关紧要 - 浏览器不会呈现多个连续的空格(这就是为什么我们有 

If the output target is something else, then you can simply cleanup the output. As you can see from other replies, there are a myriad of ways to do this. It seems like you're working with echo statements so the output-bufferingfunctions will be the route you want to take.

如果输出目标是其他目标,那么您可以简单地清理输出。正如您从其他回复中看到的那样,有无数种方法可以做到这一点。看起来您正在使用 echo 语句,因此输出缓冲函数将是您想要采用的路径。

ob_start();

echo "Hello \n";
echo "World!";

$output = preg_replace( "/ +/", ' ', str_replace( "\n", ' ', ob_get_clean() ) );

echo $output;

回答by Yacoby

If you wanted to be able to do it for anything you could use the output buffer:

如果您希望能够为任何事情做这件事,您可以使用输出缓冲区:

ob_start();

echo "Hello\n World";

$out = ob_get_contents();

ob_end_clear();
echo str_replace('\n', '', $out);

You could even use httaccess to append scripts containing this to any script called.

您甚至可以使用 httaccess 将包含此内容的脚本附加到任何调用的脚本中。

However, couldn't you just deal with it before it is set to stdout? Like

但是,您不能在设置为标准输出之前处理它吗?喜欢

function print2($str){
    echo str_replace("\n", '', $str);
}

回答by Brian

This is not a direct answer to his exact question, but to what the title seems to allude to: outputting a PHP "backspace" character, which is probably only useful when using the PHP CLI.

这不是对他的确切问题的直接回答,而是标题似乎暗示的内容:输出PHP “退格”字符,这可能仅在使用 PHP CLI 时才有用。

You can find the right ASCII code for this (and other characters) on the ASCII table, and then use either chr()or an escape sequence:

您可以在ASCII table上找到此(和其他字符)的正确 ASCII 代码,然后使用chr()或转义序列:

echo chr(8);
echo "0";

回答by Pascal MARTIN

What about just replacing the "\n" by a white space (or just nothing, if you already have one space in your incoming string)?

仅用\n空格替换“ ”怎么样(或者什么也不替换,如果您的传入字符串中已经有一个空格)

Like this, for instance :

像这样,例如:

$str = "Hello\nWorld!";
var_dump($str);

$str = str_replace("\n", ' ', $str);
var_dump($str);

The first output gives :

第一个输出给出:

string 'Hello
World!' (length=12)

And the second one :

第二个:

string 'HelloWorld!' (length=11)

Is that not enough ?
(Or maybe I don't understand the question well)

这还不够吗?
(或者也许我不太明白这个问题)

回答by p4bl0

Can't you do it like this:

你不能这样做:

$str = str_replace("\n", ' ', $str);
while (strpos($str, '  ') !== false) // while there's two spaces in a row
  $str = str_replace('  ', ' ', $str);

Now $strwill have every spaces or \ncharacters sequences replaced by only one space. (because if you just remove \n you migth have some place where a space is missing, and if you just replace it by a space you'll have some places with multiple spaces in a row).

现在$str将每个空格或\n字符序列仅替换为一个空格。(因为如果你只是删除 \n 你可能会有一些地方缺少空格,如果你只是用一个空格替换它,你会在一些地方连续有多个空格)。

EDIT: i don't know if the loop is really necessary but i don't have anything to test here if str_replacewill automatically do the trick (and i don't think using regexp for such a simple thing is really a good idea).

编辑:我不知道循环是否真的有必要,但我没有任何东西可以在这里测试是否str_replace会自动完成这个技巧(而且我不认为将正则表达式用于这么简单的事情真的是个好主意)。

回答by Alex Weinstein

Instead of thinking about how to do a backspace character, I would suggest rethinking the problem. Why do you want to take back some of your output? Probably because you outputted it too early.

与其考虑如何做退格字符,我建议重新考虑这个问题。你为什么要收回你的一些输出?可能是因为你输出的太早了。

Instead of doing an echo on the intermediary values, append them to the buffer. Then edit the buffer. Then print it out. If you can't control whether the output is produced or not (for example, you're using external vendor's library that outputs stuff), use PHP output buffering.

不要对中间值进行回显,而是将它们附加到缓冲区。然后编辑缓冲区。然后打印出来。如果您无法控制是否生成输出(例如,您正在使用输出内容的外部供应商库),请使用 PHP 输出缓冲。

回答by dvance12

I don't know how to do a backspace, but if you are just trying to do a new line I would use:

我不知道如何退格,但如果您只是想换行,我会使用:

echo "<br>";

PHP code allows for html code as long as it is used in a print or echo statement and is inside double quotes.

PHP 代码允许使用 html 代码,只要它在打印或回显语句中使用并且在双引号内。