ohdang888 Posted March 8, 2008 Share Posted March 8, 2008 i want to delete curse words from user submitted comments. any suggestions about how would i go about doing that??? I don't even know what to search for in google. Quote Link to comment https://forums.phpfreaks.com/topic/95121-censoring-comments/ Share on other sites More sharing options...
fert Posted March 8, 2008 Share Posted March 8, 2008 $bad_words=array("bad word1","bad word2"); foreach($bad_words as $b) { $text=str_replace($b,"****",$text) } Quote Link to comment https://forums.phpfreaks.com/topic/95121-censoring-comments/#findComment-487272 Share on other sites More sharing options...
ohdang888 Posted March 8, 2008 Author Share Posted March 8, 2008 thanks! Quote Link to comment https://forums.phpfreaks.com/topic/95121-censoring-comments/#findComment-487279 Share on other sites More sharing options...
corbin Posted March 8, 2008 Share Posted March 8, 2008 When you run that, you'll notice that in certain situations.... like harrassment it might become harr****mess. You could always just do ' word ' instead of 'word' though..... Oh.... Then you could have problems with 'ass.' or 'ass!' for example.... hmmm.... Edit: You could use regular expression word boundries I guess...... <?php $str = "ass harrass."; $bad = array('ass'); foreach($bad as $k => $word) { $bad[$k] = '/\b' . $word . '\b/'; } echo preg_replace($bad, '****', $str); //**** harrass ?> Quote Link to comment https://forums.phpfreaks.com/topic/95121-censoring-comments/#findComment-487309 Share on other sites More sharing options...
ohdang888 Posted March 8, 2008 Author Share Posted March 8, 2008 i guess that means i have a LOT of variations in my bad word part. Quote Link to comment https://forums.phpfreaks.com/topic/95121-censoring-comments/#findComment-487315 Share on other sites More sharing options...
aximbigfan Posted March 9, 2008 Share Posted March 9, 2008 Why not just add a space around " ass "? Chris Quote Link to comment https://forums.phpfreaks.com/topic/95121-censoring-comments/#findComment-487335 Share on other sites More sharing options...
corbin Posted March 9, 2008 Share Posted March 9, 2008 "You're an ass." Haha sorry.... I love irony. In that case for example, ass wouldn't be replaced. Quote Link to comment https://forums.phpfreaks.com/topic/95121-censoring-comments/#findComment-487337 Share on other sites More sharing options...
aximbigfan Posted March 9, 2008 Share Posted March 9, 2008 Good point. Hmm..... Maybe changing it so to if punctuation/spaces marks surround "ass" on one or both sides? Chris Quote Link to comment https://forums.phpfreaks.com/topic/95121-censoring-comments/#findComment-487396 Share on other sites More sharing options...
ohdang888 Posted March 9, 2008 Author Share Posted March 9, 2008 how would i do that? Quote Link to comment https://forums.phpfreaks.com/topic/95121-censoring-comments/#findComment-487402 Share on other sites More sharing options...
aximbigfan Posted March 9, 2008 Share Posted March 9, 2008 $pad; $pad[0] = " "; $pad[1] = "\""; $pad[2] = "'"; ect... and then add $pad on each side of the bad words var in the replace function, I guess. Chris Quote Link to comment https://forums.phpfreaks.com/topic/95121-censoring-comments/#findComment-487486 Share on other sites More sharing options...
corbin Posted March 9, 2008 Share Posted March 9, 2008 <?php $str = <<<H This is a sentence with ass in it. This is with ass at the end ass. 'ass' "ass" "Ass" !ass! ass harrass ass/ass ASS AsS aSS H; $bad = array('ass'); foreach($bad as $k => $word) { $bad[$k] = '/\b' . $word . '\b/i'; } echo preg_replace($bad, '****', $str); ?> Output: This is a sentence with **** in it. This is with **** at the end ****. '****' "****" "****" !****! **** harrass ****/**** **** **** **** Regular expression word boundries.... Quote Link to comment https://forums.phpfreaks.com/topic/95121-censoring-comments/#findComment-487490 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.