StripWords Custom Output Modifier for MODX

Here’s a nice custom output modifier developed by MODX Forum userMODX logo 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.:

[code language=”php”]
$string = $input;
$output = str_replace(array(‘these’, ‘words’, ‘will’, ‘be’, ‘stripped’), ” , $string);
return $output;
[/code]

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

[code language=”html”]
[[*content:StripWords]]
[/code]

 

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:

[code language=”html”]
[[*content:Stripwords=`these,words,will,be,stripped`]]
[/code]

[code language=”php”]
$words = array_map(‘trim’, explode(‘,’, $options));
return = str_replace($words, ” , $input);
[/code]

 

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:

[code language=”php”]
$words = $modx->getOption(‘wordsToStrip’, $scriptProperties, ”);
$words = array_map(‘trim’, explode(‘,’, $words));
return = str_replace($words, ” , $input);
[/code]

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:

[code language=”html”]
these,words,will,be,stripped
[/code]

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

[code language=”php”]
$words = $modx->getChunk(‘wordsToStrip’);
$words = array_map(‘trim’, explode(‘,’, $words));
return = str_replace($words, ” , $input);
[/code]

 

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.

[code language=”php”]
$words = $modx->getChunk(‘wordsToStrip’);
$words = array_map(‘trim’, explode(‘,’, $words));
$content = $resource->getContent();
$content = str_replace($words, ” , $content);
$resource->setContent($content);
$resource->save();
return ”;
[/code]

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:

[code language=”php”]
return = str_replace($words, ‘[expletive deleted]’ , $input);
[/code]

 

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.

 


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 <a
href=”http://bit.ly/YgFGHl”>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 *