I need a str_replace or anything that act after first element like this:
"Hello-World-I-am-Pr-Pro"
and That's Result I want:
"Hello-World I am Pr Pro"
I need a str_replace or anything that act after first element like this:
"Hello-World-I-am-Pr-Pro"
and That's Result I want:
"Hello-World I am Pr Pro"
The PHP regex engine will do that for you using preg_replace:
function replaceViaRegex($needle, $to, $haystack)
{
$regexStr = '/'.preg_quote($needle, '/').'/';
$result = preg_replace($regexStr, $to, $haystack, 1);
return $result;
}
$foo = "Hello-World-I-am-Pr-Pro";
$foo2 = replaceViaRegex('-', ' ', $foo);
echo $foo2;
Alternately, you could do
$foo = "Hello-World-I-am-Pr-Pro";
$regex = '/-/';
$foo2 = preg_replace($regex, ' ', $foo, 1);
... but that is far less flexible. Or even
$foo2 = preg_replace('/-/', ' ', "Hello-World-I-am-Pr-Pro", 1);
... but I've had trouble making preg_replace work in the past w/out a variable so I just avoid making it that dense.
I made the function above extremely clear and simple so that you could follow the logic and modify as you choose. You are far better off working with the first version than the other two if you want good, maintainable code.