here this topic: Using str_replace so that it only acts on the first match? now I would need this with the str_ireplace function
Asked
Active
Viewed 33 times
-1
-
Could you paste the code you have tried? – Michael Dec 21 '16 at 09:47
-
`preg_replace` would be enough. No need of all those *str_replace_first* – RomanPerekhrest Dec 21 '16 at 09:47
1 Answers
1
Maybe setting a "i" modifier is enough:
function str_ireplace_first($from, $to, $subject)
{
$from = '/'.preg_quote($from, '/').'/i';
return preg_replace($from, $to, $subject, 1);
}
echo str_ireplace_first('abc', '123', 'abcdef abcdef abcdef');
( it's the same answer from @karim79, but adding a final "i" in the $from
variable ).

lpg
- 4,897
- 1
- 16
- 16