I need to replace only the first three matches from a string with another string
$strings = "Apple Orange Mango Orange Grapes Apple Orange Banana Orange Dates Orange";
it should be output like :
Apple Strawberry Mango Strawberry Grapes Apple Strawberry Banana Orange Dates Orange
I have tried
$strings = str_replace("Orange", "Strawberry", $strings);
but it replaces all occurrences in the string.
You can use preg_replace for this ,
$strings = "Apple Orange Mango Orange Grapes Apple Orange Banana Orange Dates Orange";
$strings = preg_replace("/Orange/","Strawberry",$strings,3);
echo $strings;
It will output as you wish