Issue Task Closed

#54500 Use arrays in str_replace() calls

Michiel Roos 12/18/13, 22:37 updated 10/02/18, 12:07

Description

Multiple consecutive calls to str_replace can be replaced with a single call if the subject is the same.

This will speed up the replacement and in some cases has the benefit of being able to return or use the result of the str_replace directly without creating an intermediate variable.

Bad:
$lala = ‘something’;
$lala = str_replace(‘so’, ‘the’, $lala);
$lala = str_replace(‘me’, ‘ ‘, $lala);
$lala = str_replace(‘thing’, ‘other’, $lala);

Good:
$lala = str_replace(
array(‘so’, ‘me’, ‘thing’),
array(‘the’, ‘ ‘, ‘other’),
‘something’
);

Reviews 1