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

  • barry
  • Graduate
  • Graduate
  • User avatar
  • Joined: Jan 28, 2005
  • Posts: 115
  • Loc: scotland
  • Status: Offline

Post May 2nd, 2010, 7:58 am

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
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post May 2nd, 2010, 7:58 am

  • X3ndou
  • Proficient
  • Proficient
  • User avatar
  • Joined: Nov 06, 2004
  • Posts: 263
  • Loc: New Jersey
  • Status: Offline

Post May 2nd, 2010, 12:35 pm

Why not just use substr?

Code: [ Select ]
$output = substr($input, 3);
"On the day *I* go to work for Microsoft, faint oinking sounds will be heard from far overhead, the moon will not merely turn blue but develop polkadots, and hell will freeze over so solid the brimstone will go superconductive." -Eric S. Raymond
  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13458
  • Loc: Florida
  • Status: Offline

Post May 2nd, 2010, 12:46 pm

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);
Strong with this one, the sudo is.
  • barry
  • Graduate
  • Graduate
  • User avatar
  • Joined: Jan 28, 2005
  • Posts: 115
  • Loc: scotland
  • Status: Offline

Post May 3rd, 2010, 3:07 am

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 168 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
 
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.