PHP Match the entire word
<p>I have a list of bad words in the database. Everytime a user submit a comment, a function goes through the entire list of bad words and replace each word with <em>*</em></p>

<pre><code>  $query  = "SELECT * FROM bad_words ORDER BY id ASC";
  $result = mysql_query($query);    
  while($row = mysql_fetch_array($result, MYSQL_ASSOC))
  {
   $word =  $row['word'];
   $replacement =  "***";

      $userInput = str_replace(" $word ", $replacement." ", $userInput);
  } 
</code></pre>

<p>The problem is that str_replace does not work properly. For instance, "associated" will become "***ociated". I have also tried to use this preg_replace</p>

<pre><code>$userInput = preg_replace("|\\b$\word\\b|i",$replacement,$userInput);
</code></pre>

<p>but it is not working for some reason. Some of the bad words in the database contains characters like
 <code>@ | , ! * ) . ^ ' ( @</code> </p>

<p>my guess is that these characters are causing preg_replace to fail. Is there anyway around this?</p>
