We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 11798
    • 43 Posts
    Hi there,

    since I am not a well experienced php developer, I wanted to ask if there is any possibility to replace words before general documents will be renderd or published, like that solution offered in Jot.
    I haven’t found any snippet or plugin yet, but I would be useful, if some of the editors would use crtical words in their texts. (Yes, I know, they’re allowed to edit and create documents, so they should keep an eye on their work.)
    In my case it would already be helpful if Ditto is capable of doing that, but I did not get the right solution yet.

    Please let me know, if there are any ideas.

    Many thanks in advance. smiley
      • 3749
      • 24,544 Posts
      I don’t know of any existing add-on (though there may be one), but a pretty simple plugin that’s triggered by the OnBeforeDocFormSave event should do it.

      You could put the bad words as a comma delimited list in a chunk and parse them into an array in the plugin, but it would be faster to just put the words in the plugin code, something like this:

      <?php
      
      /* BadWords Plugin Code */
      
      global $content;
      
      $badWords = array(
           'badword1' => 'badword1-replacement',
           'badword2' => 'badword2-replacement',
           'badword3' => 'badword3-replacement'
       );
      
      $content = str_replace( array_keys($badWords), array_values($badWords), $content);
      ?>


      Note that str_replace() is case sensitive so if the bad words might appear at the beginning of a sentence, you’d need to duplicate them:

      'badword' => 'badwordreplacement',
      'Badword => 'Badwordreplacement'


      str_ireplace() is case insensitive but won’t preserve the original case. You can write your own replacement function that preserves the case, but it will slow things down.

      Here’s some great information on how plugins work and how to create one: http://sottwell.com/how-plugins-work.html

      Hope this helps. smiley

        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 11798
        • 43 Posts
        WOW! Thank you so much, Bob Ray!

        I never thought, it is this easy! Thanks also to the outstanding open structure of CMF. It’s the best platform ever.  smiley
        Furthermore it’s a very good example to learn how this works. I will try it today.

        By the way: I ’scanned’ the whole repository of MODx add-ons yesterday and there was none, yet.

        Thanks again!
          • 3749
          • 24,544 Posts
          As I often say, don’t thank me until it works. wink
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 11798
            • 43 Posts
            Hi Bob Ray,

            it definitely works! I tried it. It’s just wonderful and simple to use. Why did you say the chunk badword list is not that fast? Did you talk about the render time or the time to get that thing started?

            Thanks again. wink
              • 3749
              • 24,544 Posts
              Quote from: Hardoman at Apr 01, 2009, 08:38 PM

              Hi Bob Ray,

              it definitely works! I tried it. It’s just wonderful and simple to use. Why did you say the chunk badword list is not that fast? Did you talk about the render time or the time to get that thing started?

              Thanks again.  wink


              Glad it worked for you.  smiley

              On putting the words in a chunk: you might not notice the speed difference. It would be a little slower, though, because the plugin would have to retrieve the word list and then parse it before doing all the replacements. The code would also be slightly trickier because you’d have to either use two separate delimiters and parse pairs of words to create the associative array (or do the replacements as they are read), or have two matching lists of words, a badword list and a matching replacement list that would have to have the words in exactly the same order.  It might pay off if you often have to edit the list and prefer to edit a chunk. I’d just as soon have the speed and edit the plugin code directly.
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting
                • 11798
                • 43 Posts
                Many thanks for your additional info!

                And you’re right: ususally there will be no need to change a badword list often. Furthermore, you can edit the plugin code quickly, if it’s clearly laid out like you did.
                So, it’s better to use the quicker version to get a better performance.

                MODx - the mighty tool! grin