Page 1 of 1

Replace several instances to a char in string

Posted: Thu Mar 10, 2011 6:23 pm
by pedroz
$string = "123-[toB][toB][toB]-567-[toB]-78-[toB][toB]";
// should echo 123-b-567-b-78-b

How can I replace the [toB] into a letter b even it is repeated?

str_replace('[toB]','b', $string);
gives me 123-bbb-567-b-78-bb

but need // 123-b-567-b-78-b

Re: Replace several instances to a char in string

Posted: Thu Mar 10, 2011 7:12 pm
by pedroz
something like..
preg_replace('/[toB]{2,}/','b',$string);