Page 1 of 1

Replacing a character in a string

Posted: Thu Apr 02, 2009 5:54 pm
by Anarking
Good evening.


I currently have a string that has values that are seperated by '|'

Because of some strange anomalies in a loop sometimes the '|' doubles up to appear as '||' or even '|||'


Is there any way to simply search the string for occurnces of the two and three | and replace them with a single |?



Thanks in advance

Re: Replacing a character in a string

Posted: Thu Apr 02, 2009 6:01 pm
by requinix
Curious why that's a good idea, but whatever.

Code: Select all

$new = preg_replace('/\|\|+/', '|', $old);

Re: Replacing a character in a string

Posted: Thu Apr 02, 2009 6:09 pm
by Anarking
That does it, cheerrrs our felllaa

Re: Replacing a character in a string

Posted: Thu Apr 02, 2009 8:47 pm
by Chris Corbyn
McInfo wrote:Wouldn't it be better to find out why the pipes are doubling up?
Agreed, it's never a good idea to fix a bug by adding ad-hoc code that post-processes to remove the result of the bug. It gets messy very quickly.