random1 Posted July 25, 2008 Share Posted July 25, 2008 I have the bad word censoring function code below: function censorWords($search_words, $replacement , $message) { /* Usage: $searchwords = array("hat","dog","cat"); // Censored words $message = "The dog had a hat the cat was jealous of hatting."; $replacement = "*"; echo ($page->censorWords($searchwords, $replacement, $message)); */ foreach ($search_words as $search_word) { $length = strlen($search_word); $stringreplacement = ""; for( $i = 0; $i < $length; $i++ ) { $stringreplacement .= $replacement; } $message = eregi_replace("($search_word)", $stringreplacement , $message); } return $message; } I have not included any actual curse words in this example. This function is almost working but when I run it using the usage data that I've given it returns: The *** had a *** the *** was jealous of ***ting. How can I make it censor the rest of a censored word? e.g. output: The *** had a *** the *** was jealous of ******. instead? Link to comment https://forums.phpfreaks.com/topic/116583-php-langauge-censoring-function/ Share on other sites More sharing options...
JonnoTheDev Posted July 25, 2008 Share Posted July 25, 2008 You require a more complex regex specifying word boudaries. The best tool I have found as regex can be complex is regex buddy. Check it out: http://www.regexbuddy.com/ This tool you will use more than once for sure and is definately worth getting even if the regex for your query is posted by another user. Link to comment https://forums.phpfreaks.com/topic/116583-php-langauge-censoring-function/#findComment-599473 Share on other sites More sharing options...
Xurion Posted July 25, 2008 Share Posted July 25, 2008 Word boundaries in regex are \b Link to comment https://forums.phpfreaks.com/topic/116583-php-langauge-censoring-function/#findComment-599474 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.