Search and Replace with a Utility Snippet

Perform search-and-replace operations on all resources with this utility snippet

In the last article, we discussed how to create and run utility snippets. In this one, we'll look at a utility snippet designed to perform search and replace operations on all resources. This is a lot easier than you might think.

MODX logo

The Snippet

In the previous article, our example snippet looped through all resources on the site. We'll do that again, but with some added code to perform the search and replace. At one time, MODX was expressed as modX. If you have an old MODX site, you might still have the old orthography lurking around on a few pages. We'll use that for our example.

$searchPhrase = 'modX'; /* This is what we're looking for */
$replacePhrase = 'MODX'; /* This is what we'll replace it with */

$docs = $modx->getCollection('modResource');
$count = 0;
$replacements = 0;

foreach ($docs as $doc) {
    $content = $doc->getContent();
    if (strpos($content, $searchPhrase) !== false) {
        $content = str_replace($searchPhrase, $replacePhrase, $content, $replacements);
        $count+= $replacements;

return '<h3>Replaced' . $count . 'strings</h3>';

The code is pretty straightforward, but there are a couple of things to note. We could have just run str_replace on the content of every resource. Since most of them won't contain the search string, though, we first look for it with str_pos(), which is much faster than str_replace. That way our str_replace() line won't run unless it's actually needed. Notice that we've also put the save() in a place where it will only execute if there was something to replace.

The fourth argument to str_replace is optional. If there's a variable there, str_replace will set it to the number of replacements made. We used the $replacements variable for this. After saving the resource, we add that to the $count variable, which will always hold the total number of replacements made by the snippet.

Because the snippet only operates on the content field, you don't have to worry about it modifying pagetitles, aliases, or other fields of the resource.

Adapting the Snippet

You might have other strings to replace. Modifying the snippet to replace them couldn't be much simpler. Just change the $searchPhrase and $replacePhrase values.

If either of your phrases include double quotation marks, it will work fine, since we've enclosed the phrases in single quotes. If either phrase contains a single quote or a single quote used as an apostrophe, you'll need to change them to double quotes, otherwise PHP will complain.

If you have multiple strings you'd have to modify the code, but it's complicated. We'll look at how to do that in the next article, but sometimes it's easier just to run the snippet several times with modified variables at the top.

Coming Up

In the next article, we'll see how to do multiple search operations in one pass.

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 high-quality, MODX-friendly hosting? As of May 2016, Bob's Guides is hosted at A2 hosting. (More information in the box below.)

Comments (0)

Please login to comment.