Replacing Strings with an Associative Array (PHP)

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 alwayMODX logos 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.

[code language=”php”]
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:

[code language=”html”]
‘hell’ -> ‘heck’
‘damn’ -> ‘darn’
‘MODx’ -> ‘MODX’

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

[code language="php"]
$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:

[code language=”php”]
$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:

[code language=”php”]
inline strReplaceAssoc($replace, $subject) {
str_replace(array_keys($replace), array_values($replace), $subject);


[code language=”php”]
#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.


For more information on how to use MODX to create a web site, see my web site Bob’s Guides, or
better yet, buy my book: MODX: The Official Guide.

Looking for quality MODX Web Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , | Posted under MODX, MODX | RSS 2.0

Author Spotlight

Bob Ray

Bob Ray is the author of MODX: The Official Guide and over 30 MODX add-on components. He hosts Bob's Guides, a source of valuable information for MODX users, and has been very active in the MODX Forums with over 19,000 posts.

Leave a Reply

Your email address will not be published. Required fields are marked *