Replacing Strings with an Associative Array (PHP)

Use an associative array to do multiple string-replace operations

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 you only want to replace things if certain conditions apply and the tests for those conditions might not all be in the same place.

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 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 it's code is quite small and elegant. There are many versions of this on the web, so it's impossible to give credit here. 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);


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

'hell' -> 'heck'
'damn' -> 'darn'
'MODx' -> 'MODX'

<p>Here's some example code that makes those replacements using our function above:</p>

<pre class="brush: php; toolbar: false;"><fixedpre>
$content = 'Some Content with strings to be replaced';
$replace = array();

/* Some code here */

$replace['hell'] = 'heck';

/* More code */

$replace['damn'] = 'darn';

/* More code */

$replace['MODx'] = 'MODX';

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

Speed Considerations

The 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);


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);


#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, 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.

Comments (0)

Please login to comment.