helraizer Posted October 15, 2007 Share Posted October 15, 2007 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 Quote Link to comment Share on other sites More sharing options...
marcus Posted October 15, 2007 Share Posted October 15, 2007 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 ****, **** Quote Link to comment Share on other sites More sharing options...
helraizer Posted October 15, 2007 Author Share Posted October 15, 2007 Had to adapt it slightly, it worked!! Thanks for that. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.