StripWords Custom Output Modifier

Remove selected words with a custom output modifier

Here's a nice custom output modifier developed by MODX Forum user michelle84. It removes or replaces unwanted words or phrases and can be used with any tag in MODX revolution.

The Snippet

Here is michelle84's original code.:

$string = $input;
$output = str_replace(array('these', 'words', 'will', 'be', 'stripped'), '' , $string);
return $output;

To use it, you simply create a snippet called StripWords, paste the code above into the snippet, and use a tag like this:


Some Alternatives

You may object to having to edit the snippet code to change the list of stripped words, especially if it's a non-tech-savvy client who is doing the editing. One option would be to send the words to strip as a property in the snippet tag:

$words = array_map('trim', explode(',', $options));
return = str_replace($words, '' , $input);

The array_map() line above, demonstrates a useful function in PHP. The array_map() function takes an array as its second argument. It runs the function named in the first argument on each member of the array. It returns the altered array.

In the example above, array_map() calls the trim() function on each member of the array created by explode(). The trim() function removes any extraneous leading or trailing spaces as well as a few other things like tabs and linefeeds.

The trouble with this option is that when you want to change the word list, you need to edit every instance of the StripWords output modifier on the site. No one wants that. One alternative would be to create a default property of the snippet called 'wordsToStrip' and put the comma-separated list of words as its value. In that case, the snippet would look like this:

$words = $modx->getOption('wordsToStrip', $scriptProperties, '');
$words = array_map('trim', explode(',', $words));
return = str_replace($words, '' , $input);

This might still challenge some users. A slightly slower, but more user-friendly way to go would be to put the word list in a chunk called wordsToStrip. All the user needs to do is edit the chunk to change the word list. If you are filtering out offensive words and phrases, this is handy, since site visitors will always be coming up with new ones.

Here's the wordsToStrip chunk:


Now, our snippet needs a small alteration to get the words from the chunk:

$words = $modx->getChunk('wordsToStrip');
$words = array_map('trim', explode(',', $words));
return = str_replace($words, '' , $input);

Speed Considerations

If page-load speeds are a concern, it's *much* faster to alter the text when it's being saved to the database than to make the modifications on each page load, though it's not always practical. If the text you want to modify is the content field of a resource or is created in NewsPublisher, you can attach a plugin called StripWords to the OnDocFormSave System Event with code like this. This version, like the one above, uses the chunk to get the words.

$words = $modx->getChunk('wordsToStrip');
$words = array_map('trim', explode(',', $words));
$content = $resource->getContent();
$content = str_replace($words, '' , $content);
return '';

Unfortunately, this technique doesn't work with either Articles or Quip (at least not in the current versions). Quip does allow hooks, however, so it's possible that you could do it in a Quip hook.

Replacing the Strings

In all the examples above, the unwanted strings are replaced with an empty string. If you'd rather replace them with something else, you can change the empty string to whatever you want, like this:

return = str_replace($words, '[expletive deleted]' , $input);

Coming Up

What if you want to replace each string with a different value? That requires creating two separate arrays for str_replace() to use. We'll look at how to do that in the next article.


Comments (0)

Please login to comment.