I just wondering how I could replace the second instance of a string inside a string in php such as follows:
a - b - c
Where it woulld add an extra space after the second "-" but only if it finds 2.
$finds = explode('-', "a - b - c");
if (count($finds) == 3) {
$finds[2] = " {$finds[2]}";
}
$finds = implode('-', $finds);
$str ="a - b - c";
if (substr_count($str,"-")>2){
print preg_replace("/^(.*)-(.*)-(.*)/","\\1-\\2- \\3",$str);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With