tags:

views:

951

answers:

7

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

+1  A: 

Hi,

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)

Pascal MARTIN
+1  A: 

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

p4bl0
My example above was crude, but the \n is unavoidable.
Jeff
+2  A: 

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);
}
Yacoby
+1  A: 

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"

Brenton Alker
That shows up as 'ab[funny-character]' on my output screen.
Jeff
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.
Brenton Alker
When viewing the source of the web page with FF view source.
Jeff
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.
Brenton Alker
+2  A: 

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;
Peter Bailey
A: 

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.

Alex
A: 

I verified "That shows up as 'ab[funny-character]' on my output screen. – Jeff".

My reason for wanting such is that I made a seconds counter and don't want linefeeds or to continue sprawling across screen. Think stop watch, or any in-place counter.

mugger