Replacing Strings with an Associative Array

Using a PHP associative array to replace words in our StripWords output modifier

By Bob Ray  |  July 2, 2024  |  3 min read
Replacing Strings with an Associative Array

In the previous article, we looked at using str_replace with two separate arrays, one for the strings to replace, and one for the replacements. This isn't always the most convenient way to do it, especially if the strings and their replacements are scattered throughout your code. Sometimes, it's nice to be able to just create an associative array where each key/value pair is a word to be replaced, and its replacement.

In this article we'll look at a function that uses an associative array of keys and values to do string replacement.

Replacements Array

Suppose that you want to do multiple replacements to some text, but the strings and their replacements are scattered throughout your code. It's much easier to write or build an associate array where every key is a word or phrase to be replaced and the value is the replacement.

Here's a function and some example code that demonstrates how to do that. We'll call it StrReplaceAssoc() and its code is quite small and elegant. There are many versions of this on the web, so it's impossible to give credit here. To be honest, I'm not sure whether I found it or created it.

public function strReplaceAssoc(array $replace, $subject) {
    return str_replace(array_keys($replace), array_values($replace), $subject);
}

Usage

We'll use the strings and replacements from my previous article:

$replace = array(
    'hell' => 'heck'
    'damn' => 'darn'
    'MODx' => 'MODX'
};

Here's some example code that makes those replacements using our function above:

$content = 'Some Content with strings to be replaced';

$replace = array(
    'hell' => 'heck'
    'damn' => 'darn'
    'MODx' => 'MODX'
};

$content = strReplaceAssoc($replace, $content);

Speed Considerations

There is a slight speed penalty for using this function, but sometimes the convenience outweighs the very small slowdown. Because the code is only one line, you can mitigate that penalty somewhat by putting the code inline rather than in a function, which removes the time it take to make the function call and return. In that case the last line of the code above would be:

$content = str_replace(array_keys($replace), array_values($replace), $content);

Aside

The example above makes me nostalgic for compiled languages like C++ where you could put something like either of these in a header file:

inline strReplaceAssoc($replace, $subject) {
    str_replace(array_keys($replace), array_values($replace), $subject);
}

or

#define strReplaceAssoc($replace,$subject) str_replace(array_keys($replace), array_values($replace), $subject);

In either case, the compiler would replace the strReplaceAssoc() call with the inline or defined code. Later versions of PHP have inline and anonymous functions, as well as traits, but they're really not the same. PHP's #define only works for strings, not code, but even if it did, you could only use #define if you always use the same variable names, and it would probably get you fired if you write code for a company with sensible coding guidelines.


Bob Ray is the author of the MODX: The Official Guide and dozens of MODX Extras including QuickEmail, NewsPublisher, SiteCheck, GoRevo, Personalize, EZfaq, MyComponent and many more. His website is Bob’s Guides. It not only includes a plethora of MODX tutorials but there are some really great bread recipes there, as well.