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 Link to comment https://forums.phpfreaks.com/topic/73357-solved-arrays-and-explode-help/ 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 ****, **** Link to comment https://forums.phpfreaks.com/topic/73357-solved-arrays-and-explode-help/#findComment-370111 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. Link to comment https://forums.phpfreaks.com/topic/73357-solved-arrays-and-explode-help/#findComment-370132 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.