Jump to content

[SOLVED] arrays and explode help!


helraizer

Recommended Posts

For my project of making a chatbox, I am working on implimenting a code to keep it "clean". So if someone enters a word that could be deemed offensive it changes that word to "****".

 

  
                    $dirty[0] = "rude word1";
                    $dirty[1] = "rude word2";         
                    $dirty[2] = "rude word3";
                    if (in_array($_POST['input'], $dirty)) {
                        $text = "****";
                    }

 

 

Which works, but only if the whole string is only that one word. How would I make it replace that word, in the middle of a string? Possibly using the explode function?

 

So if they wrote "Oh my rude word, that was rude word" or something it would write "Oh my ****, that was ****".

 

Hope that was clear,

 

Thanks,

Sam

Link to comment
Share on other sites

Tested and works:

 

<?php
$dirty = array(
'rude word',
'another rude phrase',
'etc'
);

$text = "This is a RUDE word, while this is another ruDe pHraSe, etc";

foreach($dirty AS $bad_word){
$text = preg_replace("/$bad_word/i","****", $text);
}

echo $text;
?>

 

Will output:

 

This is a ****, while this is ****, ****

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.