Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP "backspace" character during output possible?

Tags:

php

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 );

like image 931
Jeff Avatar asked Sep 17 '09 16:09

Jeff


2 Answers

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"

like image 152
Brenton Alker Avatar answered Sep 20 '22 17:09

Brenton Alker


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;
like image 34
Peter Bailey Avatar answered Sep 19 '22 17:09

Peter Bailey