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

  • Home
  • SEARCH
  • 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 8309201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:02:25+00:00 2026-06-08T19:02:25+00:00

i have some simple code that does a preg match: $bad_words = array(‘dic’, ‘tit’,

  • 0

i have some simple code that does a preg match:

$bad_words = array('dic', 'tit', 'fuc',); //for this example i replaced the bad words

for($i = 0; $i < sizeof($bad_words); $i++)
{
    if(preg_match("/$bad_words[$i]/", $str, $matches))
    {
        $rep = str_pad('', strlen($bad_words[$i]), '*');
        $str = str_replace($bad_words[$i], $rep, $str);
    }
}
echo $str;

So, if $str was "dic" the result will be ‘*‘ and so on.

Now there is a small problem if $str == f.u.c. The solution might be to use:

$pattern = '~f(.*)u(.*)c(.*)~i';
$replacement = '***';
$foo =  preg_replace($pattern, $replacement, $str);

In this case i will get ***, in any case. My issue is putting all this code together.

I’ve tried:

$pattern = '~f(.*)u(.*)c(.*)~i';
$replacement = 'fuc';
$fuc =  preg_replace($pattern, $replacement, $str);

$bad_words = array('dic', 'tit', $fuc,); 

for($i = 0; $i < sizeof($bad_words); $i++)
{
    if(preg_match("/$bad_words[$i]/", $str, $matches))
    {
        $rep = str_pad('', strlen($bad_words[$i]), '*');
            $str = str_replace($bad_words[$i], $rep, $str);
    }
}
echo $str;

The idea is that $fuc becomes fuc then I place it in the array then the array does its jobs, but this doesn’t seem to work.

  • 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-06-08T19:02:27+00:00Added an answer on June 8, 2026 at 7:02 pm

    First of all, you can do all of the bad word replacements with one (dynamically generated) regex, like this:

    $bad_words = array('dic', 'tit', 'fuc',);
    
    $str = preg_replace_callback("/\b(?:" . implode( '|', $bad_words) . ")\b/", 
        function( $match) {
            return str_repeat( '*', strlen( $match[0])); 
    }, $str);
    

    Now, you have the problem of people adding periods in between the word, which you can search for with another regex and replace them as well. However, you must keep in mind that . matches any character in a regex, and must be escaped (with preg_quote() or a backslash).

    $bad_words = array_map( function( $el) { 
        return implode( '\.', str_split( $el));
    }, $bad_words);
    

    This will create a $bad_words array similar to:

    array(
        'd\.i\.c',
        't\.i\.t',
        'f\.u\.c'
    )
    

    Now, you can use this new $bad_words array just like the above one to replace these obfuscated ones.

    Hint: You can make this array_map() call “better” in the sense that it can be smarter to catch more obfuscations. For example, if you wanted to catch a bad word separated with either a period or a whitespace character or a comma, you can do:

    $bad_words = array_map( function( $el) { 
        return implode( '(?:\.|\s|,)', str_split( $el));
    }, $bad_words);
    

    Now if you make that obfuscation group optional, you’ll catch a lot more bad words:

    $bad_words = array_map( function( $el) { 
        return implode( '(?:\.|\s|,)?', str_split( $el));
    }, $bad_words);
    

    Now, bad words should match:

    f.u.c
    f,u.c
    f u c 
    fu c
    f.uc
    

    And many more.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically I have some simple code that does some things for files and I'm
I have this AS2 code that does some simple animations when I rollover a
I'have written a simple code, that gets accelerometer&orientation meter and display some graphics based
I have some code that does a bunch of HTTP GETs, POSTs, and PUTs
I have simple code that does a head request for a URL and then
I have some simple code that is comparing two float values to illustrate a
Trying to get comfortable with jQuery and I have encountered some sample code that
So I have a few properties that I'm using in some sample code I'm
I have some VERY simple code to return the title for a section header:
I have simple HTML code with some JavaScript. It looks like: <html> <head> <script

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.