example: $var1='hellowordinghellowording';
I want to replace hello just once not twice inside this variable
How to replace one occurrence of a word with PHP
Moderator: General Moderators
Re: How to replace one occurrence of a word with PHP
Code: Select all
function str_replace_once($str_pattern, $str_replacement, $string){
if (strpos($string, $str_pattern) !== false){
$occurrence = strpos($string, $str_pattern);
return substr_replace($string, $str_replacement, strpos($string, $str_pattern), strlen($str_pattern));
}
return $string;
} Re: How to replace one occurrence of a word with PHP
Got it, with a loop