Replacing profanity with more appropriate words

Continuing the discussion from Php code to replace bad words in a document with *** and maintain break line:

One way to do this:

function makeFriendly(string &$str) {

    // Edit this array to make it your own
    $regex = array(
        '/f**k/i' => 'sc**w',
        '/s**t/i' => 'rub**sh',
        '/p***y/i' => 'fluffy bunny rabbit'   
    );
    
    $disallowed = [];
    foreach ($regex as $key => $value) {
        $x = explode('/', $key);
        $disallowed[] = $x[1];
    }
      
    $str = strtolower($str);
    $str = explode(' ', $str);
    $output = [];
    foreach ($str as $word) {
        if(in_array($word, $disallowed)){
            foreach ($regex as $key => $value) {
                if(preg_match($key, $word)){
                    $output[] = $value;  
                } 
            }      
        } else {
            $output[] = $word;  
        }
    }
    $str = implode(' ', $output);
} # End of function

# Using the function
$example = ‘f**k this s**t’;
makeFriendly($example);
echo $example; #Output: sc**w this rub**sh

I dont have access to the rest of the posts in this subject, so i may be missing requirements, but…

    $regex = array(
        '/f**k/i' => 'sc**w',
        '/s**t/i' => 'rub**sh',
        '/p***y/i' => 'fluffy bunny rabbit'   
    );
   $str = preg_replace(array_keys($regex), array_values($regex),$str);

?
Preg_Replace takes arrays.

1 Like

Wow ! I sure do like to make life difficult for myself. Thank you so much for your contribution. Your solution is a lot neater, and is clearly the one i should be using. I can’t believe it was that simple. Thanks again :blush:

1 Like

I am not sure that that would result in hello being changed but if it does then the algorithm needs improvement.

Not the algorithm, the patterns need refining.
/hell/i too open, will hit words like ‘hello’ and ‘shell’.
/\shell\s/i finds just the word in question, etc.

Yeah that is what I meant. I apologize for being careless about terminology. I assumed munchermods understood.

1 Like

Thank you SamuelCalifornia , REGEX is not my strongest subject, and neither is PHP evidently :neutral_face: That was really my first attempt to solve a problem for myself. I knew in my heart of hearts that it was not right though. It was too messy. I’m glad i decided to upload the code, you guy’s have helped me so much. I’m still struggling to get this right though. The suggestion by m_hutley to change the pattern to /\shell\s/i does work, but it will only match the last instance of that word. If the word appears several times, then it fails. I’m not sure if the pattern still needs refining, or if i need to add some functionality in PHP to make this work? Any suggestions are most welcome.

I do not use regular expressions much so I cannot help with the details but for multiple occurrences I assume we can just call preg_replace multiple times. However preg_replace has the limit parameter implying it will replace multiple occurrences so perhaps you just need help with the use of preg_replace.

Note that the PHP compiler uses re2c and Bison to compile PHP. Use of those would be the the most sophisticated way to do what you are doing but the generated code is C or C++ and require more time to learn than I am sure you want to spend. But I am suggesting it because it would be the best solution if you wanted to spend time on it.

To that extent, it is likely someone else has been here before and has developed something you can use.