$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
something like.. preg_replace('/[toB]{2,}/','b',$string);
That would require at least two occurences (from {2,} ) of [toB]. You should just match [toB] and replace each such occurence.
$p = "#\[toB]#"; $s = preg_replace($p, 'b', $string);