Hey, 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 would add an extra space after the second "-
" but only if it finds 2.
Thanks
Hey, 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 would add an extra space after the second "-
" but only if it finds 2.
Thanks
Substring the string starting at the index of the first dash using strpos
then do a str_replace
on the rest of the string. Concatenate the two together.
$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);
}