15

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:

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

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

Hello World!

... thus removing the \n.

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

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

0

8 Answers 8

35

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

echo 'ab' . chr(8);

will output "a"

7
  • 7
    In a browser? I only tested it on the command line, doesn't really make sense to backspace in a browser I don't think. Commented Sep 17, 2009 at 17:11
  • When viewing the source of the web page with FF view source.
    – Jeff
    Commented Sep 17, 2009 at 17:14
  • 6
    Ok, if you're talking for anything other than CLI you probably don't want a literal backspace character. The other suggested solutions are probably more useful. Commented Sep 17, 2009 at 17:17
  • 1
    thanks for the straightforward answer to this question - exactly what I was looking for :) Commented Apr 7, 2011 at 6:00
  • This doesn't work when redirecting to a file on the command line. I get the same problem as the web source.
    – jbo5112
    Commented May 31, 2013 at 10:28
6

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

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-buffering functions will be the route you want to take.

ob_start();

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

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

echo $output;
3

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.

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

function print2($str){
    echo str_replace("\n", '', $str);
}
1
  • 1
    did you mean ob_end_clean(); by chance?
    – MrMesees
    Commented Sep 24, 2016 at 10:05
3

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.

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:

echo chr(8);
echo "\010";
1

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

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)

1

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 $str will have every spaces or \n characters 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).

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

2
  • My example above was crude, but the \n is unavoidable.
    – Jeff
    Commented Sep 17, 2009 at 17:06
  • For what reason do you not think using regexp is a good idea? If performance is a concern, surely your loop is a potential issue or am I missing something? Commented Feb 9, 2014 at 15:03
0

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.

-6

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.