0

Trying to figure out a way to perform string manipulation in php. In the example below I need to recognize all instances of [backspace] and remove them from the string, BUT I also need to remove the character immediately before it as well.

$string = "this is a sentence with dog[backspace][backspace][backspace]cat in it";

would become "this is a sentence with cat in it".

My initial thought was turning the string into an array and performing the operation that way somehow as I don't believe there is any way I could do this with str_replace.

$array = str_split($string);

foreach($array as $key)
{
   .. lost here
}
3
  • So you are saying that you need to treat the [backspace] as a literal backspace operation? So since you have 3 of those in a row here it deletes d-o-g? Commented Jan 15, 2014 at 23:48
  • You can't use a find-replace solution here you would have to make it actually run this as code, so when [backspace] is detected it would remove 1 letter before it. Commented Jan 15, 2014 at 23:49
  • Correct Mike... it would treat it as a literal backspace. I just used backspace as an example, but others I would be adding to the string as well. Commented Jan 15, 2014 at 23:51

3 Answers 3

3
<?php
$string = "this is a sentence with dog[backspace][backspace][backspace]cat in it";
do{
 $string = preg_replace('~[^]]\[backspace\]~', '', $string, -1, $count);
} while($count);

echo $string;

If you are not using literal [backspace] then same concept -

$string = "this is a sentence with dogXXXcat in it";


do{
  $string = preg_replace('~[^X]X~', '', $string, -1, $count);
} while($count);

echo $string;
Sign up to request clarification or add additional context in comments.

Comments

0

Okay this isn't a good solution overall, but I found backspace can be represented as a character in PHP.

Such as

$string = str_replace("[backspace]", chr(8), $string);

This wont work for outputting in a webbrowser it will show up a strange character, works for using PHP in a command prompt though.

Comments

0

I would think you could just create a loop that executes until there are no more backspaces present, taking the first instance of it an deleting it along with the preceding character.

function perform_backspace ($string = '') {
    $search = '[backspace]';
    $search_length = strlen($search);
    $search_pos = strpos($string, $search);
    while($search_pos !== false) {
        if($search_pos === 0) {
            // this is beginning of string, just delete the search string
            $string = substr_replace($string, '', $search_pos, $search_length);
        } else {
            // delete character before search and the search itself
            $string = substr_replace($string, '', $search_pos - 1, $search_length + 1);
        }
        $search_pos = strpos($string, $search);
    }
    return $string;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.