Home / How to use an associative array with PHP’s str_replace function

How to use an associative array with PHP’s str_replace function

If you are a PHP developer, I’m sure you know the str_replace function for replacing strings and are probably aware that you can pass an array of strings as the search strings and an array as the replacements. What this post looks at is a way of using a single associative array instead of two arrays, which makes dealing with a large number of replacements much more maintainable in your code.

Using two arrays

A quick overview of how str_replace works with arrays first:

mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )

The search and replace parameters can either be strings or arrays. If both are arrays, the string in index 0 in the first array will be replaced with the string in index 0 in the second array, index 1 with index 1 and so on. The example below replaces ‘quick’ with ‘hungry’, ‘fox’ with ‘wolf’ and so on.

$string = "The quick brown fox jumped over the lazy dog";
$search = array('quick', 'fox', 'jumped over', 'dog');
$replace = array('hungry', 'wolf', 'ate', 'mouse');
echo str_replace($search, $replace, $string);

Here is the resulting output:

The hungry brown wolf ate the lazy mouse

Using an array and a string

If the search parameter is an array and the replace parameter a string, everything matching each of the array items in the source string will be replaced with the replace parameter.

Using the same example code as the above, to replace all the items in the $search array with [replaced] do this:

echo str_replace($search, "[replaced]", $string);

Which would result in this:

The [replaced] brown [replaced] [replaced] the lazy [replaced]

Using an associative array

Now finally the point of this post: to show how to simplify the code using an associative array. If the number of searches and replacements is going to be quite large it can be difficult to see in the arrays which item is replaced with the other item.

Using an associative array makes this a lot simpler and instead of creating two arrays we can do this instead, where the array index (e.g. "quick") will be replaced with the value (e.g. "hungry"):

$replacements = array(
    'quick' => 'hungry',
    'fox' => 'wolf',
    'jumped over' => 'ate',
    'dog' => 'mouse'
);

At a quick glance it’s now very easy to see what will be replaced with what, and should the replacements need to be changed in the future it’s very easy to do.

str_replace expects two regular arrays and not an associative array. Use array_keys to extract the keys from the associative array and pass those for the search parameter. The array itself can be passed as the replace parameter:

echo str_replace(array_keys($replacements), $replacements, $string);

This results in the same output as the first example:

The hungry brown wolf ate the lazy mouse

It will take a little more processing than using two separate arrays but the end result is much easier to maintain and read code when dealing with a large number of string replacements.