Hi gang I would appreciate some help with the censor words script that I am attempting to create. So far I have managed to create this. $str = "This is an assessment"; $banned_words = "ass|asswipe" $censor_words = explode("|", $banned_words); foreach ($censor_words as $word) { $str = preg_replace("/\b($word)\b/", str_repeat("*", strlen($word)), $str); } PHP: The way I have this at the moment, it will censor "ass" and "asswipe" providing that they are used on their own and not part of another word. How would I change this so that it will also censor them when they are part of another word like "assessment" would appear as "***essment". Thanks Phil
"/$word/ig" "i" makes it case insensitive and "g" makes it greedy so all instances are replaced. This is generally a futile excercise as there are a million and one ways to be vulgar.
$str = preg_replace("/\b($word)\b/", str_repeat("*", strlen($word)), $str); Remove the \b from the regex - \b matches a word boundary. You probably do want to go case-insensitive but you don't need the g modifier - in terms of regex, greedy/ungreedy relates to quantifiers, not the number of replacements to make. By default, when you use a +, * or ? symbol, it will match as much as possible (hence greedy).
Thanks for the responce thats help a lot. How do you add 2 checks in the regex function? Say if i wanted to use \b and the \i for case-insensitive. would it be like this "/\b\i($word)\b\i/" or something else?
Ahh I eventually found it on the PHP website hidden away in the function manuals. \b will do the boundries and (?i) will do the case-insensitive. Thanks for the help.
\b is part of the pattern itself and goes where needed. i is a pattern modifier that goes after the delimiter and applies to the whole pattern. "/\b($word)\b/i"