php how can i replace a character then the very next charact

  • barry
  • Graduate
  • Graduate
  • User avatar
  • Posts: 115
  • Loc: scotland

Post 3+ Months Ago

Hi all. I am trying to remove a character and then the very next character from a string but the following character is never the same character.

Here is what i meen

I have a series of strings like this



$output = "^2 bob"
$output ="^t jim"
$output ="^y tom"
$output ="^4 fred"

No i know i can use

str_replace("^","",$output);

will give me

2 bob
t jim
y tom
4 fred

and this will replace the ^ in all of them one after the other

I want

bob
jim
tom
fred

how do i write it so i can replace the ^ and then the very next character ?

any help would be great!
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • X3ndou
  • Proficient
  • Proficient
  • User avatar
  • Posts: 263
  • Loc: New Jersey

Post 3+ Months Ago

Why not just use substr?

Code: [ Select ]
$output = substr($input, 3);
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

If I go strictly by your description, I would use this, which will remove the carrot and the following character no matter what it is.

PHP Code: [ Select ]
preg_replace('#\^.#s', '', $str);


If I assume you only want this to apply if the carrot is actually followed by another character that isn't a space, I would use this.

PHP Code: [ Select ]
preg_replace('#\^\S#', '', $str);


If I assume you want that space between them removed too, I would use this.

PHP Code: [ Select ]
preg_replace('#\^\S #', '', $str);


If I can assume that these will always be at the very beginning of the string, I would use this.

PHP Code: [ Select ]
substr($str, 3);
  • barry
  • Graduate
  • Graduate
  • User avatar
  • Posts: 115
  • Loc: scotland

Post 3+ Months Ago

sweet thanks much!!

I got it working using a different method but this looks like it will work better

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 84 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.