...

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



maltrecho
11-07-2003, 05: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.

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

http://be.php.net/manual/en/function.str-replace.php


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

http://be.php.net/manual/en/function.ereg-replace.php

maltrecho
11-07-2003, 12:47 PM
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:


<?php
$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"

mordred
11-08-2003, 12:10 AM
You mean like this?



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

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

maltrecho
11-08-2003, 12:31 AM
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.

mordred
11-08-2003, 03: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);

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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum