简体   繁体   中英

Remove before character (like backspace)

I have some text like this: "I am a gaot[BS][BS][BS]oat"

And It should look like this: "I am a goat"

How can I use "[BS]" as 'backspace'? So remove the character before backspace. I tried preg_replace but it replaced only 1 backspace.

Can someone help?

I tried this:

$text = preg_replace('/.{1}\[BS\]/', '', $text);

But this works only with 1 [BS].

There are two ways of doing this that I can see.

The "easy but long" way:

do {
    $text = preg_replace("/.\[BS\]/","",$text,-1,$c);
} while($c);

The "voodoo shortcut" way:

$text = preg_replace("/.(?R)*\[BS\]/","",$text);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM