PHP censor words function

This php function takes a list of words you define and censors them. In the example you have an array of banned words and a $replace variable that is used as a replacement of the censored words.
By using eregi_replace function we can check every word in the array and replace it with the desired, proper code.

  1. <?php
  2.  
  3. $banned = array("visit", "http://", "www.", ".com");
  4. $replace = '<span style="color:red; font-style: italic;">×××</span>';
  5.  
  6. function censor($content) {
  7.     global $banned, $replace;
  8.  foreach($banned as $words) {
  9.  $content= eregi_replace($words, $replace, $content);
  10.  }
  11. return $content;
  12. }
  13.  
  14. echo censor("Visit us at http://www.phpjunk.com");
  15.  //or if you store data into a variable:
  16. echo censor($data);
  17. ?>

No related php tutorials.

bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark bookmark
tabs-top

Leave a Reply