View Full Version : function to filter rude-words, please

11-07-2003, 04:39 AM
I want to include a function in a page to filter all rude words submited by users. The filter should be something like (i.e.):

Words beginning by "abc", should be replaced with "****" or "oops" or whatever, so having an array like (i.e.):

$badwords = array(abc,aei,xyz);

How would the function look like to replace "abcde" and "aeiou" to "oops" when the input is (i.e.):

$text = "Hello abcde, what's up?. Your bro is an aeiou. Byeee"

So the output would be: "Hello oops, what's up?. Your bro is an oops. Byeee"

Is that clear? Thanks gurus.

11-07-2003, 11:09 AM
If you need to simply replace words in a string there are functions for that


if you need to do something a litte more complex then use REGEX


11-07-2003, 11:47 AM
The problem is that I need to replace all the words beginning with (i.e.) "foo","fee"... not just full words like "food","feet"... and I don't have a clue about how to write a regexp like that. :confused:

This is what I've got so far:

$text = "while footing, I broke my feet";
echo str_replace(array("foo","fee"), "oops", $text);

The output is "while oopsting, I broke my oopst", but I want to have "while oops I broke my oops"

11-07-2003, 11:10 PM
You mean like this?

$text = "while footing, I broke my feet";

echo preg_replace(array("/foo.*?\b/i", "/fee.*?\b/i"), "oops", $text);

11-07-2003, 11:31 PM
That's giving me while oopsroke my feet an it's really a pity because I just can't understand anything about reg exp to try new things.

11-08-2003, 02:09 AM
Sorry, the forum software ate my backslashes. I should have known better... anyway, try this again:

$text = "while footing, I broke my feet";

echo preg_replace(array("/foo.*?\b/i", "/fee.*?\b/i"), "oops", $text);

11-08-2003, 02:49 AM
Wow! :p That's great. From the deep :thumbsup:

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum