title: PHP swear word filter
body: <p>I'm working on a WordPress plugin that replaces the bad words from the comments with random new ones from a list.</p>
  
  <p>I now have 2 arrays: one containing the bad words and another containing the good words.</p>
  
  <pre><code>$bad = array("bad", "words", "here");
  $good = array("good", "words", "here");
  </code></pre>
  
  <p>Since I'm a beginner, I got stuck at some point.</p>
  
  <p>In order to replace the bad words, I've been using <code>$newstring = str_replace($bad, $good, $string);</code>.</p>
  
  <p>My first problem is that I want to turn off the case sensivity, so I won't put the words like this <code>"bad", "Bad", "BAD", "bAd", "BAd", etc</code> but I need the new word to keep the format of the original word, for example if I write "Bad", it would be replaced with "Words", but if I type "bad", it would be replaced with "words", etc.</p>
  
  <p>My first tought was to use <code>str_ireplace</code>, but it forgets if the original word had a capital letter.</p>
  
  <p>The second problem is that I don't know how to deal with the users that type like this: "b a d", "w o r d s", etc. I need an idea.</p>
  
  <p>In order to make it select a random word, I think I can use <code>$new = $good[rand(0, count($good)-1)];</code> then <code>$newstring = str_replace($bad, $new, $string);</code>. If you have a better idea, I'm here to listen.</p>
  
  <p>The general look of my script:</p>
  
  <pre><code>function noswear($string)
  {
      if ($string)
      {       
          $bad = array("bad", "words");
          $good = array("good", "words"); 
          $newstring = str_replace($bad, $good, $string);     
          return $newstring;
  }
  
  echo noswear("I see bad words coming!");
  </code></pre>
  
  <p>Thank you in advance for your help!</p>