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
https://forums.phpfreaks.com/topic/73357-solved-arrays-and-explode-help/
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 ****, ****

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.