Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3599588
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T20:23:11+00:00 2026-05-18T20:23:11+00:00

I have a list of bad words in the database. Everytime a user submit

  • 0

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 *

  $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);
  } 

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

$userInput = preg_replace("|\\b$\word\\b|i",$replacement,$userInput);

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

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T20:23:11+00:00Added an answer on May 18, 2026 at 8:23 pm

    Putting aside issues of whether this is a good idea, implementation is pretty simple:

    $userInput = preg_replace('/\b' . preg_quote($word, '/') . '\b/i', $replacement, $userInput);
    

    You can probably also improve performance by using a single replacement:

    $words = array();
    $replacement =  "***";
    $query  = "SELECT * FROM bad_words ORDER BY id ASC";
    $result = mysql_query($query);    
    while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
        $words[] =  preg_quote($row['word'], '/');
    } 
    
    $userInput = preg_replace('/\b(' . implode('|', $words) . ')\b/i', $replacement, $userInput);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of bad words in the database. Everytime a user submit
I have some text i need to filter out a list of bad words
I have a list of bad words having around 450 words in it. I'm
I have a bad (spam) words list for spam protection filters. I want to
I have a bad words filter that uses a list of keywords saved in
i have list of rows that user select and i want to delete them,
I have list of words. I type in a word misspelled. Can I query
I have a list of words that I want to exclude from some query
Not very technical, but... I have to implement a bad words filter in a
I have to implement a bad words filter on my website, which is a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.