Search code examples
phpstringtrimstrstrbackspace

echo backspace inside string


Pretty simple but I'm looking for the easiest way (HEX?) and it's not working...

I want to add to the string backspaces (delete last character)...

Here is my simple code :

<?php

    echo '<br>Delete me!!!'."\x8"."\x8"."\x8"."\x8"."\x8"."\x8"."\x8"."\x8"."\x8"."\x8"."\x8"."\x8";

?>

Small tweak needed here ;)


Solution

  • backspace is a special character that is interpreted by editors and various text input boxes as a request to remove the character before the cursor. The backspace character itself has no magical powers, it cannot make disappear something that was already displayed.

    If you need to remove some characters from a string in PHP you can use substring(), str_replace(), preg_replace() or other string handling function.

    For example, you can ignore the last 3 characters from a string like this:

    echo(substr('blah-blah', 0, -3));