-1

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

Community
  • 1
  • 1
John Smith
  • 6,129
  • 12
  • 68
  • 123

1 Answers1

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