Forum Moderators: coopster
function str_replace_once($needle , $replace , $haystack){
// Looks for the first occurence of $needle in $haystack
// and replaces it with $replace.
$pos = strpos($haystack, $needle);
if ($pos === false) {
// Nothing found
return $haystack;
}
return substr_replace($haystack, $replace, $pos, strlen($needle));
}
any short versions? I have:
$show = str_replace('term','replacement',$string);
and read somewhere that there was a value you could enter to reduce the number of replacements to one i.e.
$show = str_replace('term','replacement',$string, 1);
countargument is not a limit but count of the number of times the needle was found and replaced. To use a limit go with preg_replace [php.net].
print preg_replace("/ll/", "", "good golly miss molly!", 1);
// good goy miss molly!